github.com/Kong/go-pdk@v0.11.0/service/response/response.go (about) 1 /* 2 Manipulation of the response from the Service. 3 */ 4 package response 5 6 import ( 7 "github.com/Kong/go-pdk/server/kong_plugin_protocol" 8 "google.golang.org/protobuf/types/known/structpb" 9 "github.com/Kong/go-pdk/bridge" 10 ) 11 12 // Holds this module's functions. Accessible as `kong.ServiceResponse` 13 type Response struct { 14 bridge.PdkBridge 15 } 16 17 // Called by the plugin server at initialization. 18 // func New(ch chan interface{}) Response { 19 // return Response{bridge.New(ch)} 20 // } 21 22 // kong.ServiceResponse.GetStatus() returns the HTTP status code 23 // of the response from the Service as an integer. 24 func (r Response) GetStatus() (i int, err error) { 25 return r.AskInt(`kong.service.response.get_status`, nil) 26 } 27 28 // kong.ServiceResponse.GetHeaders() returns a map holding the headers 29 // from the response from the Service. Keys are header names. 30 // Values are either a string with the header value, or an array of strings 31 // if a header was sent multiple times. Header names in this table are 32 // case-insensitive and dashes (-) can be written as underscores (_); 33 // that is, the header X-Custom-Header can also be retrieved as x_custom_header. 34 // 35 // Unlike kong.Response.GetHeaders(), this function will only return headers 36 // that were present in the response from the Service (ignoring headers added 37 // by Kong itself). If the request was not proxied to a Service 38 // (e.g. an authentication plugin rejected a request and produced an HTTP 401 response), 39 // then the returned headers value might be nil, since no response 40 // from the Service has been received. 41 // 42 // The max_args argument specifies the maximum number of returned headers. 43 // Must be greater than 1 and not greater than 1000, or -1 to specify the 44 // default limit of 100 arguments. 45 func (r Response) GetHeaders(max_headers int) (map[string][]string, error) { 46 if max_headers == -1 { 47 max_headers = 100 48 } 49 50 arg := kong_plugin_protocol.Int{ V: int32(max_headers) } 51 out := new(structpb.Struct) 52 err := r.Ask(`kong.service.response.get_headers`, &arg, out) 53 if err != nil { 54 return nil, err 55 } 56 57 return bridge.UnwrapHeaders(out), nil 58 } 59 60 // kong.ServiceResponse.GetHeader() returns the value of the specified response header. 61 // 62 // Unlike kong.Response.GetHeader(), this function will only return a header 63 // if it was present in the response from the Service 64 // (ignoring headers added by Kong itself). 65 func (r Response) GetHeader(name string) (string, error) { 66 return r.AskString(`kong.service.response.get_header`, bridge.WrapString(name)) 67 } 68 69 // kong.ServiceResponse.GetRawBody() returns the raw body 70 // of the response from the Service. 71 func (r Response) GetRawBody() ([]byte, error) { 72 out := new(kong_plugin_protocol.RawBodyResult) 73 err := r.Ask(`kong.service.response.get_raw_body`, nil, out) 74 if err != nil { 75 return nil, err 76 } 77 return out.GetContent(), nil 78 }