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

Error sending request with httpc, with array form

Open Dongss opened this issue 1 year ago • 1 comments

Describe the bug

Sending request with httpc, with array form ?status=["Processing", "Cancelled"]

Server got error:

fullName: `status`, error: `string: `[Processing Cancelled]`, error: `invalid character 'P' looking for beginning of value``

To Reproduce

  1. The code is API defination:
   type MyRequest struct {
	Status   []string `form:"status,optional"` 
   }

code:



 req := types.MyRequest{
     Status: []string{"Processing",  "Cancelled"}
 }

 httpc.Do(ctx, "GET", "localhost:80", req)
  1. The error is server side got error:

    fullName: `status`, error: `string: `[Processing Cancelled]`, error: `invalid character 'P' 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: [Linux]
  • go-zero version [1.6.3]
  • goctl version [1.5.6]

More description

The problem seems like happening here: fmt.Sprint(v)

https://github.com/zeromicro/go-zero/blob/d5302f2dbe1ea23812c4775bedcbe6626f7641fb/rest/httpc/requests.go#L60

Dongss avatar Sep 09 '24 02:09 Dongss

Use json tag and post json instead.

 type MyRequest struct {
	Status   []string `json:"status,optional"` 
   }

kesonan avatar Sep 18 '24 09:09 kesonan

Using this definition, in the old version (1.5.5), form ?status=["a","b"] this will parse normally, in the new version (1.8.0) parsing error, but in 1.8.0 using form ?status=a,b or ?status=a&status=b will parse normally

xuerbujia avatar Feb 06 '25 09:02 xuerbujia

Using this definition, in the old version (1.5.5), form status:["a","b"] this will parse normally, in the new version (1.8.0) parsing error, but in 1.8.0 using form status: a,b will parse normally

I've used this method before and I can't upgrade the go-zero version because of this issue

xuerbujia avatar Feb 06 '25 09:02 xuerbujia