123456789101112131415161718192021222324252627282930313233343536373839 |
- package tools
- import (
- "compress/gzip"
- "compress/zlib"
- "golang.org/x/net/html/charset"
- "io"
- "net/http"
- )
- func DecodeHttpResponse(response *http.Response) ([]byte, error) {
- var (
- contentEncoding = response.Header.Get("Content-Encoding")
- contentType = response.Header.Get("Content-Type")
- reader io.Reader = response.Body
- )
- if "gzip" == contentEncoding {
- if r, err := gzip.NewReader(response.Body); err == nil {
- defer r.Close()
- reader = r
- }
- } else if "deflate" == contentEncoding {
- if r, err := zlib.NewReader(response.Body); err == nil {
- defer r.Close()
- reader = r
- }
- }
- if "application/octet-stream" != contentType {
- if r, err := charset.NewReader(reader, contentType); err == nil {
- reader = r
- } else {
- return nil, err
- }
- }
- return io.ReadAll(reader)
- }
|