4
解析http响应报文时,由于http响应报文传输编码是Transfer-Encoding: chunked http响应报文头里没有ContentLength 导致json反序列化响应报文报错 field XXX is not set
原因如下,
// 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)
}