1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00
MiraiGo/utils/http.go
2020-07-27 03:52:38 +08:00

33 lines
615 B
Go

package utils
import (
"bytes"
"compress/gzip"
"io/ioutil"
"net/http"
"strings"
)
func HttpGetBytes(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if strings.Contains(resp.Header.Get("Content-Encoding"), "gzip") {
buffer := bytes.NewBuffer(body)
r, _ := gzip.NewReader(buffer)
unCom, err := ioutil.ReadAll(r)
return unCom, err
}
return body, nil
}