diff --git a/client.go b/client.go index f166509..ab7c00b 100644 --- a/client.go +++ b/client.go @@ -195,10 +195,23 @@ func (c *client) sendRequest( } if !c.contentEncoding.IsZero() { - body, err = c.encoder.Encode(body) - if err != nil { - return nil, internalError.WithErrCode(ErrCodeMarshalRequest, - fmt.Errorf("failed to marshal with json.Marshal: %w", err)) + // Get the data from the buffer before encoding + var bufData []byte + if buf, ok := body.(*bytes.Buffer); ok { + bufData = buf.Bytes() + encodedBuf, err := c.encoder.Encode(bytes.NewReader(bufData)) + if err != nil { + if buf, ok := body.(*bytes.Buffer); ok { + c.bufferPool.Put(buf) + } + return nil, internalError.WithErrCode(ErrCodeMarshalRequest, + fmt.Errorf("failed to encode request body: %w", err)) + } + // Return the original buffer to the pool since we have a new one + if buf, ok := body.(*bytes.Buffer); ok { + c.bufferPool.Put(buf) + } + body = encodedBuf } } }