go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

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

Open giantcroc opened this issue 1 year ago • 5 comments
trafficstars

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"`
  }
  1. 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):

  • OS: [e.g. Linux]
  • go-zero version 1.6.5
  • goctl version goctl version 1.6.6 linux/amd64

More description Add any other context about the problem here.

giantcroc avatar Jun 26 '24 10:06 giantcroc

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

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

Owen-Zhang avatar Jun 27 '24 10:06 Owen-Zhang

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"`
  }

Issues-translate-bot avatar Jun 27 '24 10:06 Issues-translate-bot

都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.

giantcroc avatar Jun 28 '24 04:06 giantcroc

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

kesonan avatar Jun 29 '24 04:06 kesonan

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?

giantcroc avatar Jul 01 '24 07:07 giantcroc