mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-27 12:28:40 +08:00
a987512c7b
* feat: more meaningful error messages, close body on httpc requests * fix: test failure
40 lines
1004 B
Go
40 lines
1004 B
Go
package httpc
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/zeromicro/go-zero/core/mapping"
|
|
"github.com/zeromicro/go-zero/rest/internal/encoding"
|
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
|
)
|
|
|
|
// Parse parses the response.
|
|
func Parse(resp *http.Response, val interface{}) error {
|
|
if err := ParseHeaders(resp, val); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ParseJsonBody(resp, val)
|
|
}
|
|
|
|
// ParseHeaders parses the response headers.
|
|
func ParseHeaders(resp *http.Response, val interface{}) error {
|
|
return encoding.ParseHeaders(resp.Header, val)
|
|
}
|
|
|
|
// ParseJsonBody parses the response body, which should be in json content type.
|
|
func ParseJsonBody(resp *http.Response, val interface{}) error {
|
|
defer resp.Body.Close()
|
|
|
|
if withJsonBody(resp) {
|
|
return mapping.UnmarshalJsonReader(resp.Body, val)
|
|
}
|
|
|
|
return mapping.UnmarshalJsonMap(nil, val)
|
|
}
|
|
|
|
func withJsonBody(r *http.Response) bool {
|
|
return r.ContentLength > 0 && strings.Contains(r.Header.Get(header.ContentType), header.ApplicationJson)
|
|
}
|