fix message body parsing

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-04-09 22:55:01 +03:00
parent 9150958044
commit 21a41a8e03

14
util.go
View File

@ -160,16 +160,24 @@ func newTemplate(path string) (util.Template, error) {
} }
func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error { func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
// fast path return
if hrsp.StatusCode == http.StatusNoContent {
return nil
}
b, err := ioutil.ReadAll(hrsp.Body) b, err := ioutil.ReadAll(hrsp.Body)
if err != nil { if err != nil {
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
} }
if hrsp.StatusCode < 400 { if hrsp.StatusCode < 400 {
// unmarshal only if body not nil
if len(b) > 0 {
// unmarshal // unmarshal
if err := cf.Unmarshal(b, rsp); err != nil { if err := cf.Unmarshal(b, rsp); err != nil {
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
} }
}
return nil return nil
} }
@ -187,8 +195,12 @@ func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp inte
return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode)) return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
} }
if len(b) > 0 {
if cerr := cf.Unmarshal(b, err); cerr != nil { if cerr := cf.Unmarshal(b, err); cerr != nil {
return errors.InternalServerError("go.micro.client", cerr.Error()) err = errors.InternalServerError("go.micro.client", cerr.Error())
}
} else {
err = errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
} }
return err return err