From 5cdae38666023314b74b2553475afd47171cb9ac Mon Sep 17 00:00:00 2001 From: Strift Date: Tue, 18 Feb 2025 14:19:12 +0800 Subject: [PATCH] fix: handle buffer encoding correctly for Go 1.16 compatibility --- client.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index f1665097..ab7c00b7 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 } } }