Skip to content

Commit

Permalink
always set accept encoding header (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
devsergiy authored Oct 26, 2023
1 parent b4e3ab1 commit 4877432
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions v2/pkg/engine/datasource/httpclient/nethttpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import (
const (
ContentEncodingHeader = "Content-Encoding"
AcceptEncodingHeader = "Accept-Encoding"
AcceptHeader = "Accept"
ContentTypeHeader = "Content-Type"

EncodingGzip = "gzip"
EncodingDeflate = "deflate"

ContentTypeJSON = "application/json"
)

var (
Expand Down Expand Up @@ -90,16 +97,18 @@ func Do(client *http.Client, ctx context.Context, requestInput []byte, out io.Wr
request.URL.RawQuery = query.Encode()
}

request.Header.Add("accept", "application/json")
request.Header.Add("content-type", "application/json")
request.Header.Add(AcceptHeader, ContentTypeJSON)
request.Header.Add(ContentTypeHeader, ContentTypeJSON)
request.Header.Set(AcceptEncodingHeader, EncodingGzip)
request.Header.Add(AcceptEncodingHeader, EncodingDeflate)

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

respReader, err := respBodyReader(request, response)
respReader, err := respBodyReader(response)
if err != nil {
return err
}
Expand All @@ -108,17 +117,13 @@ func Do(client *http.Client, ctx context.Context, requestInput []byte, out io.Wr
return
}

func respBodyReader(req *http.Request, resp *http.Response) (io.ReadCloser, error) {
if req.Header.Get(AcceptEncodingHeader) == "" {
return resp.Body, nil
}

func respBodyReader(resp *http.Response) (io.ReadCloser, error) {
switch resp.Header.Get(ContentEncodingHeader) {
case "gzip":
case EncodingGzip:
return gzip.NewReader(resp.Body)
case "deflate":
case EncodingDeflate:
return flate.NewReader(resp.Body), nil
default:
return resp.Body, nil
}

return resp.Body, nil
}

0 comments on commit 4877432

Please sign in to comment.