Files
ddns-go/util/http_util.go
jeessy2 eec98404fe feat: support english (#967)
* feat: init the i18n

* fix: porkbun

* refactor: http_util GetHTTPResponse

* fix: save

* fix: add more i18n

* feat: i18n at writing.html

* feat: README EN

* fix: default api

* fix: pic
2024-01-11 15:52:53 +08:00

46 lines
879 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package util
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// GetHTTPResponse 处理HTTP结果返回序列化的json
func GetHTTPResponse(resp *http.Response, err error, result interface{}) error {
body, err := GetHTTPResponseOrg(resp, err)
if err == nil {
// log.Println(string(body))
if len(body) != 0 {
err = json.Unmarshal(body, &result)
}
}
return err
}
// GetHTTPResponseOrg 处理HTTP结果返回byte
func GetHTTPResponseOrg(resp *http.Response, err error) ([]byte, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
lr := io.LimitReader(resp.Body, 1024000)
body, err := io.ReadAll(lr)
if err != nil {
return nil, err
}
// 300及以上状态码都算异常
if resp.StatusCode >= 300 {
err = fmt.Errorf(LogStr("返回内容: %s ,返回状态码: %d", string(body), resp.StatusCode))
}
return body, err
}