@@ -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 {
@@ -171,15 +178,17 @@ type graphResponse struct {
171
178
172
179
// Request is a GraphQL request.
173
180
type Request struct {
174
- q string
175
- vars map [string ]interface {}
176
- files []file
181
+ q string
182
+ vars map [string ]interface {}
183
+ files []file
184
+ Header Header
177
185
}
178
186
179
187
// NewRequest makes a new Request with the specified string.
180
188
func NewRequest (q string ) * Request {
181
189
req := & Request {
182
- q : q ,
190
+ q : q ,
191
+ Header : make (map [string ][]string ),
183
192
}
184
193
return req
185
194
}
@@ -201,6 +210,37 @@ func (req *Request) File(fieldname, filename string, r io.Reader) {
201
210
})
202
211
}
203
212
213
+ // A Header represents the key-value pairs in an HTTP header.
214
+ type Header map [string ][]string
215
+
216
+ // Add adds the key, value pair to the header.
217
+ // It appends to any existing values associated with key.
218
+ func (h Header ) Add (key , value string ) {
219
+ textproto .MIMEHeader (h ).Add (key , value )
220
+ }
221
+
222
+ // Set sets the header entries associated with key to
223
+ // the single element value. It replaces any existing
224
+ // values associated with key.
225
+ func (h Header ) Set (key , value string ) {
226
+ textproto .MIMEHeader (h ).Set (key , value )
227
+ }
228
+
229
+ // Get gets the first value associated with the given key.
230
+ // It is case insensitive; textproto.CanonicalMIMEHeaderKey is used
231
+ // to canonicalize the provided key.
232
+ // If there are no values associated with the key, Get returns "".
233
+ // To access multiple values of a key, or to use non-canonical keys,
234
+ // access the map directly.
235
+ func (h Header ) Get (key string ) string {
236
+ return textproto .MIMEHeader (h ).Get (key )
237
+ }
238
+
239
+ // Del deletes the values associated with key.
240
+ func (h Header ) Del (key string ) {
241
+ textproto .MIMEHeader (h ).Del (key )
242
+ }
243
+
204
244
// file represents a file to upload.
205
245
type file struct {
206
246
Field string
0 commit comments