From 87385b6384c7e23fe3dd18417dfb7205afbea319 Mon Sep 17 00:00:00 2001 From: Marco Bardelli Date: Wed, 24 Jan 2024 01:56:47 +0100 Subject: [PATCH] fix: decompress resonse when appropriate, fixes #46 Signed-off-by: Marco Bardelli --- response.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/response.go b/response.go index 37972ba..17f5388 100644 --- a/response.go +++ b/response.go @@ -17,6 +17,8 @@ package koios import ( + "compress/flate" + "compress/gzip" "encoding/json" "fmt" "io" @@ -81,7 +83,20 @@ func ReadResponseBody(rsp *http.Response) (body []byte, err error) { defer func() { _ = rsp.Body.Close() }() - return io.ReadAll(rsp.Body) + rb := rsp.Body + + if strings.Contains(rsp.Header.Get("Content-Encoding"), "gzip") { + if rb, err = gzip.NewReader(rsp.Body); err == nil { + defer rb.Close() + } else { + return nil, err + } + } else if rsp.Header.Get("Content-Encoding") == "deflate" { + rb = flate.NewReader(rsp.Body) + defer rb.Close() + } + + return io.ReadAll(rb) } // ReadAndUnmarshalResponse is helper to unmarchal json responses.