Skip to content

Commit

Permalink
fix: decompress resonse when appropriate, fixes #46
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Bardelli <[email protected]>
  • Loading branch information
safanaj authored and mkungla committed Jan 30, 2024
1 parent 680d504 commit 87385b6
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package koios

import (
"compress/flate"
"compress/gzip"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 87385b6

Please sign in to comment.