1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package http
- import (
- "compress/gzip"
- "compress/zlib"
- "golang.org/x/net/html/charset"
- "io"
- "net/http"
- )
- func DecodeResponse(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 {
- //goland:noinspection ALL
- defer r.Close()
- reader = r
- }
- } else if "deflate" == contentEncoding {
- if r, err := zlib.NewReader(response.Body); err == nil {
- //goland:noinspection ALL
- 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)
- }
|