github.com/Kong/go-pdk@v0.11.0/service/request/request.go (about) 1 /* 2 Manipulation of the request to the Service. 3 */ 4 package request 5 6 import ( 7 "github.com/Kong/go-pdk/bridge" 8 "github.com/Kong/go-pdk/server/kong_plugin_protocol" 9 "google.golang.org/protobuf/types/known/structpb" 10 ) 11 12 // Holds this module's functions. Accessible as `kong.ServiceRequest` 13 type Request struct { 14 bridge.PdkBridge 15 } 16 17 // Called by the plugin server at initialization. 18 // func New(ch chan interface{}) Request { 19 // return Request{bridge.New(ch)} 20 // } 21 22 // kong.ServiceRequest.SetScheme() sets the protocol to use 23 // when proxying the request to the Service. 24 // Supported values are "http" or "https". 25 func (r Request) SetScheme(scheme string) error { 26 return r.Ask(`kong.service.request.set_scheme`, bridge.WrapString(scheme), nil) 27 } 28 29 // kong.ServiceRequest.SetPath() sets the path component 30 // for the request to the service. It is not normalized 31 // in any way and should not include the querystring. 32 func (r Request) SetPath(path string) error { 33 return r.Ask(`kong.service.request.set_path`, bridge.WrapString(path), nil) 34 } 35 36 // kong.ServiceRequest.SetRawQuery() sets the querystring 37 // of the request to the Service. The query argument is a string 38 // (without the leading ? character), and will not be processed in any way. 39 // 40 // For a higher-level function to set the query string from a ???? of arguments, 41 // see kong.ServiceRequest.SetQuery(). 42 func (r Request) SetRawQuery(query string) error { 43 return r.Ask(`kong.service.request.set_raw_query`, bridge.WrapString(query), nil) 44 } 45 46 // kong.ServiceRequest.SetMethod() sets the HTTP method 47 // for the request to the service. 48 // 49 // Supported method values are: "GET", "HEAD", "PUT", "POST", 50 // "DELETE", "OPTIONS", "MKCOL", "COPY", "MOVE", "PROPFIND", 51 // "PROPPATCH", "LOCK", "UNLOCK", "PATCH", "TRACE". 52 func (r Request) SetMethod(method string) error { 53 return r.Ask(`kong.service.request.set_method`, bridge.WrapString(method), nil) 54 } 55 56 // kong.ServiceRequest.SetQuery() sets the querystring of the request to the Service. 57 // 58 // Unlike kong.ServiceRequest.SetRawQuery(), the query argument must be a map 59 // in which each key is a string (corresponding to an arguments name), and each 60 // value is either an array of strings or booleans. Additionally, all string 61 // values will be URL-encoded. 62 // 63 // The resulting querystring will contain keys in their lexicographical order. 64 // The order of entries within the same key is retained. 65 // 66 // If further control of the querystring generation is needed, a raw querystring 67 // can be given as a string with kong.ServiceRequest.SetRawQuery(). 68 func (r Request) SetQuery(query map[string][]string) error { 69 arg, err := bridge.WrapHeaders(query) 70 if err != nil { 71 return err 72 } 73 74 return r.Ask(`kong.service.request.set_query`, arg, nil) 75 } 76 77 // kong.ServiceRequest.SetHeader() sets a header in the request 78 // to the Service with the given value. Any existing header with 79 // the same name will be overridden. 80 // 81 // If the header argument is "host" (case-insensitive), then this 82 // will also set the SNI of the request to the Service. 83 func (r Request) SetHeader(name string, value string) error { 84 arg := kong_plugin_protocol.KV{ 85 K: name, 86 V: structpb.NewStringValue(value), 87 } 88 return r.Ask(`kong.service.request.set_header`, &arg, nil) 89 } 90 91 // kong.ServiceRequest.AddHeader() adds a request header with the given value 92 // to the request to the Service. Unlike kong.ServiceRequest.SetHeader(), 93 // this function will not remove any existing headers with the same name. 94 // Instead, several occurences of the header will be present in the request. 95 // The order in which headers are added is retained. 96 func (r Request) AddHeader(name string, value string) error { 97 arg := kong_plugin_protocol.KV{ 98 K: name, 99 V: structpb.NewStringValue(value), 100 } 101 return r.Ask(`kong.service.request.add_header`, &arg, nil) 102 } 103 104 // kong.ServiceRequest.ClearHeader() removes all occurrences 105 // of the specified header in the request to the Service. 106 func (r Request) ClearHeader(name string) error { 107 return r.Ask(`kong.service.request.clear_header`, bridge.WrapString(name), nil) 108 } 109 110 // kong.ServiceRequest.SetHeaders() sets the headers of the request 111 // to the Service. Unlike kong.ServiceRequest.SetHeader(), the headers argument 112 // must be a map in which each key is a string (corresponding to a header’s name), 113 // and each value an array of strings. 114 // 115 // The resulting headers are produced in lexicographical order. 116 // The order of entries with the same name is retained. 117 // 118 // This function overrides any existing header bearing the same name as those 119 // specified in the headers argument. Other headers remain unchanged. 120 // 121 // If the "Host" header is set (case-insensitive), then this is will also set 122 // the SNI of the request to the Service. 123 func (r Request) SetHeaders(headers map[string][]string) error { 124 arg, err := bridge.WrapHeaders(headers) 125 if err != nil { 126 return err 127 } 128 return r.Ask(`kong.service.request.set_headers`, arg, nil) 129 } 130 131 // kong.ServiceRequest SetRawBody() sets the body of the request to the Service. 132 // 133 // The body argument must be a string and will not be processed in any way. 134 // This function also sets the Content-Length header appropriately. 135 // To set an empty body, one can give an empty string "" to this function. 136 // 137 // For a higher-level function to set the body based on the request content type, 138 // see kong.ServiceRequest.SetBody(). 139 func (r Request) SetRawBody(body string) error { 140 return r.Ask(`kong.service.request.set_raw_body`, bridge.WrapString(body), nil) 141 } 142 143 // TODO set_body