[zeromicro/go-zero]http响应的传输编码是 Transfer-Encoding: chunked,导致json反序列化响应报文出错

2023-12-22 886 views
4

image 解析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)
}

回答

5

master分支已经修改