diff --git a/x-pack/filebeat/input/httpjson/policy.go b/x-pack/filebeat/input/httpjson/policy.go index 5c140eba662..ed7f25bf2c2 100644 --- a/x-pack/filebeat/input/httpjson/policy.go +++ b/x-pack/filebeat/input/httpjson/policy.go @@ -93,6 +93,7 @@ func (p *Policy) CustomRetryPolicy(ctx context.Context, resp *http.Response, err if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) { defer func() { if resp.Body != nil { + io.Copy(io.Discard, resp.Body) resp.Body.Close() } }() diff --git a/x-pack/filebeat/input/httpjson/request.go b/x-pack/filebeat/input/httpjson/request.go index 6e45487f8b3..b15f3db51b1 100644 --- a/x-pack/filebeat/input/httpjson/request.go +++ b/x-pack/filebeat/input/httpjson/request.go @@ -618,6 +618,7 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra err error urlCopy url.URL urlString string + httpResp *http.Response intermediateResps []*http.Response finalResps []*http.Response ) @@ -671,15 +672,10 @@ func (r *requester) processChainPaginationEvents(ctx context.Context, trCtx *tra } // collect data from new urls - httpResp, err := rf.collectResponse(ctx, chainTrCtx, r) + httpResp, err = rf.collectResponse(ctx, chainTrCtx, r) if err != nil { return -1, fmt.Errorf("failed to collect response: %w", err) } - defer func() { - if httpResp != nil && httpResp.Body != nil { - httpResp.Body.Close() - } - }() // store data according to response type if i == len(r.requestFactories)-1 && len(ids) != 0 {