@@ -38,6 +38,7 @@ import (
38
38
"io"
39
39
"mime/multipart"
40
40
"net/http"
41
+ "net/textproto"
41
42
42
43
"github.com/pkg/errors"
43
44
)
@@ -122,6 +123,12 @@ func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) error
122
123
}
123
124
r .Header .Set ("Content-Type" , writer .FormDataContentType ())
124
125
r .Header .Set ("Accept" , "application/json" )
126
+ for key , values := range req .Header {
127
+ for _ , value := range values {
128
+ r .Header .Add (key , value )
129
+ }
130
+ }
131
+ c .logf (">> headers: %v" , r .Header )
125
132
r = r .WithContext (ctx )
126
133
res , err := c .httpClient .Do (r )
127
134
if err != nil {
@@ -174,12 +181,34 @@ type Request struct {
174
181
q string
175
182
vars map [string ]interface {}
176
183
files []file
184
+
185
+ // Header mirrors the Header of a http.Request. It contains
186
+ // the request header fields either received
187
+ // by the server or to be sent by the client.
188
+ //
189
+ // If a server received a request with header lines,
190
+ //
191
+ // Host: example.com
192
+ // accept-encoding: gzip, deflate
193
+ // Accept-Language: en-us
194
+ // fOO: Bar
195
+ // foo: two
196
+ //
197
+ // then
198
+ //
199
+ // Header = map[string][]string{
200
+ // "Accept-Encoding": {"gzip, deflate"},
201
+ // "Accept-Language": {"en-us"},
202
+ // "Foo": {"Bar", "two"},
203
+ // }
204
+ Header Header
177
205
}
178
206
179
207
// NewRequest makes a new Request with the specified string.
180
208
func NewRequest (q string ) * Request {
181
209
req := & Request {
182
- q : q ,
210
+ q : q ,
211
+ Header : make (map [string ][]string ),
183
212
}
184
213
return req
185
214
}
@@ -201,6 +230,37 @@ func (req *Request) File(fieldname, filename string, r io.Reader) {
201
230
})
202
231
}
203
232
233
+ // A Header represents the key-value pairs in an HTTP header.
234
+ type Header map [string ][]string
235
+
236
+ // Add adds the key, value pair to the header.
237
+ // It appends to any existing values associated with key.
238
+ func (h Header ) Add (key , value string ) {
239
+ textproto .MIMEHeader (h ).Add (key , value )
240
+ }
241
+
242
+ // Set sets the header entries associated with key to
243
+ // the single element value. It replaces any existing
244
+ // values associated with key.
245
+ func (h Header ) Set (key , value string ) {
246
+ textproto .MIMEHeader (h ).Set (key , value )
247
+ }
248
+
249
+ // Get gets the first value associated with the given key.
250
+ // It is case insensitive; textproto.CanonicalMIMEHeaderKey is used
251
+ // to canonicalize the provided key.
252
+ // If there are no values associated with the key, Get returns "".
253
+ // To access multiple values of a key, or to use non-canonical keys,
254
+ // access the map directly.
255
+ func (h Header ) Get (key string ) string {
256
+ return textproto .MIMEHeader (h ).Get (key )
257
+ }
258
+
259
+ // Del deletes the values associated with key.
260
+ func (h Header ) Del (key string ) {
261
+ textproto .MIMEHeader (h ).Del (key )
262
+ }
263
+
204
264
// file represents a file to upload.
205
265
type file struct {
206
266
Field string
0 commit comments