Skip to content

Commit 12fba59

Browse files
authored
api: pass in body to NewRequestWithContext (#263)
By directly passing in the body we will also get the ContentLength header set. Minor: we avoid the extra buffer than json.NewEncoder uses, and instead directly get the buffer from json.Marshal.
1 parent a56f139 commit 12fba59

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

internal/api/api.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ func (r *request) do(ctx context.Context, result interface{}) (bool, error) {
119119
}
120120

121121
// Create the JSON object.
122-
var buf bytes.Buffer
123-
if err := json.NewEncoder(&buf).Encode(map[string]interface{}{
122+
reqBody, err := json.Marshal(map[string]interface{}{
124123
"query": r.query,
125124
"variables": r.vars,
126-
}); err != nil {
125+
})
126+
if err != nil {
127127
return false, err
128128
}
129129

130130
// Create the HTTP request.
131-
req, err := http.NewRequestWithContext(ctx, "POST", r.client.url(), nil)
131+
req, err := http.NewRequestWithContext(ctx, "POST", r.client.url(), bytes.NewBuffer(reqBody))
132132
if err != nil {
133133
return false, err
134134
}
@@ -141,7 +141,6 @@ func (r *request) do(ctx context.Context, result interface{}) (bool, error) {
141141
for k, v := range r.client.opts.AdditionalHeaders {
142142
req.Header.Set(k, v)
143143
}
144-
req.Body = ioutil.NopCloser(&buf)
145144

146145
// Perform the request.
147146
resp, err := http.DefaultClient.Do(req)

0 commit comments

Comments
 (0)