zeromicro / go-zero

A cloud-native Go microservices framework with cli tool for productivity.
https://go-zero.dev
MIT License
29.31k stars 3.96k forks source link

httpx.parse error when the field's type in request is []byte #4215

Open giantcroc opened 4 months ago

giantcroc commented 4 months ago

Describe the bug httpx.parse error when the field's type in request is []byte and I post the data body after json Marshal like '{"task":"base64 encoded data"}'.

To Reproduce Steps to reproduce the behavior, if applicable:

  1. The code is

    ApplyRequest {
    Task []byte `json:"task"`
    }
    ApplyResponse {
    ApplyResult string `json:"applyresult"`
    }
  2. The error is

    error: fullName: `task`, error: `string: `dGFza25hbWU6IHRhc2sxCmpvYmxpc3Q6Ci0gam9ibmFtZTogYnVpbGQKICBqb2J1cmw6IGh0dHA6Ly9qZW5raW5zLmV4YW1wbGUuY29tL2pvYi9CdWlsZFByb2plY3RBCiAgcGFyYW1ldGVyczoKICAgIG5vZGVsYWJlbDogbGludXgKICAgIGFyY2hpdGVjdHVyZTogeDg2XzY0CiAgICBwcm9kdWN0dHlwZTogYXBwbGljYXRpb24KICAgIHB5dGhvbnZlcnNpb246ICIzLjgiCiAgamVua2luc25hbWU6IGplbmtpbnNfbWFpbgogIHByaW9yaXR5OiAxCi0gam9ibmFtZTogdGVzdCBwcm9qZWN0IGIKICBqb2J1cmw6IGh0dHA6Ly9qZW5raW5zLmV4YW1wbGUuY29tL2pvYi9UZXN0UHJvamVjdEIKICBwYXJhbWV0ZXJzOgogICAgbm9kZWxhYmVsOiB3aW5kb3dzCiAgICBhcmNoaXRlY3R1cmU6IHg4Nl82NAogICAgcHJvZHVjdHR5cGU6IGxpYnJhcnkKICAgIHB5dGhvbnZlcnNpb246ICIzLjkiCiAgamVua2luc25hbWU6IGplbmtpbnNfdGVzdAogIHByaW9yaXR5OiAy`, error: `invalid character 'd' looking for beginning of value``

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Environments (please complete the following information):

More description Add any other context about the problem here.

Owen-Zhang commented 4 months ago

都base64了,为何不搞成string呢?

 ApplyRequest {
  Task string `json:"task"`
  }
Issues-translate-bot commented 4 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


It’s all base64, why not make it a string?

ApplyRequest {
Task string `json:"task"`
}
giantcroc commented 4 months ago

都base64了,为何不搞成string呢?

 ApplyRequest {
  Task string `json:"task"`
  }

We use json.Marshal(ApplyRequest) to process the request struct in client,and base64 encoded data is the result of this process.

kesonan commented 4 months ago

Go-zero will not help you with type conversion, so your data type must be consistent with the input parameters.

giantcroc commented 4 months ago

Go-zero will not help you with type conversion, so your data type must be consistent with the input parameters.

func unmarshalJsonReader(reader io.Reader, v any, unmarshaler *Unmarshaler) error {
// var m any
if err := jsonx.UnmarshalFromReader(reader, &v); err != nil {
return err
}
return nil
// return unmarshaler.Unmarshal(m, v)
}

I have tested to remove unmarshaler.Unmarshal, it works to process []byte correctly. So why do we need to split the unmarshal to two processes, one for string to map[string]any, another for map[string]any to v?