From 51217eae02feb1f946519252858e8c4b2172c3b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20D=C3=B6tsch?= Date: Thu, 27 Jan 2022 12:08:09 +0100 Subject: [PATCH] read full request.Body to reuse http/1 connections see https://tipsfordev.com/go-http-requests-json-reusing-connections --- api_response.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/api_response.go b/api_response.go index 9982db8..e52b3ef 100644 --- a/api_response.go +++ b/api_response.go @@ -7,6 +7,7 @@ package bitbucketv1 import ( "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "strings" @@ -668,7 +669,6 @@ func GetActivitiesResponse(r *APIResponse) (Activities, error) { // NewAPIResponse create new APIResponse from http.Response func NewAPIResponse(r *http.Response) *APIResponse { - response := &APIResponse{Response: r} return response } @@ -688,10 +688,18 @@ func NewAPIResponseWithError(r *http.Response, bodyBytes []byte, err error) (*AP // NewBitbucketAPIResponse create new API response from http.response func NewBitbucketAPIResponse(r *http.Response) (*APIResponse, error) { response := &APIResponse{Response: r} - err := json.NewDecoder(r.Body).Decode(&response.Values) + + decoder := json.NewDecoder(r.Body) + err := decoder.Decode(&response.Values) if err != nil { return nil, err } + + if decoder.More() { + // there's more data in the stream, so discard whatever is left + io.Copy(ioutil.Discard, r.Body) + } + return response, err }