Skip to content

Commit e4998c2

Browse files
committed
Gzip agent api request bodies before sending them off
1 parent c74b814 commit e4998c2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

api/client.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package api
44

55
import (
66
"bytes"
7+
"compress/gzip"
78
"context"
89
"crypto/tls"
910
"encoding/json"
@@ -224,11 +225,17 @@ func (c *Client) newRequest(
224225
u := joinURLPath(c.conf.Endpoint, urlStr)
225226

226227
buf := new(bytes.Buffer)
228+
227229
if body != nil {
228-
err := json.NewEncoder(buf).Encode(body)
230+
gzipWriter := gzip.NewWriter(buf)
231+
err := json.NewEncoder(gzipWriter).Encode(body)
229232
if err != nil {
230233
return nil, err
231234
}
235+
236+
if err := gzipWriter.Close(); err != nil {
237+
return nil, fmt.Errorf("closing gzip writer: %w", err)
238+
}
232239
}
233240

234241
req, err := http.NewRequestWithContext(ctx, method, u, buf)
@@ -260,6 +267,7 @@ func (c *Client) newRequest(
260267

261268
if body != nil {
262269
req.Header.Add("Content-Type", "application/json")
270+
req.Header.Add("Content-Encoding", "gzip")
263271
}
264272

265273
return req, nil
@@ -309,7 +317,6 @@ func newResponse(r *http.Response) *Response {
309317
// interface, the raw response body will be written to v, without attempting to
310318
// first decode it.
311319
func (c *Client) doRequest(req *http.Request, v any) (*Response, error) {
312-
313320
resp, err := agenthttp.Do(c.logger, c.client, req,
314321
agenthttp.WithDebugHTTP(c.conf.DebugHTTP),
315322
agenthttp.WithTraceHTTP(c.conf.TraceHTTP),

0 commit comments

Comments
 (0)