Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShouldBindJSON bind struct, header "content-type application/json", no Response? #4152

Open
AutumnFlowerTheLeft opened this issue Feb 8, 2025 · 0 comments

Comments

@AutumnFlowerTheLeft
Copy link

ide debug log
ShouldBindJSON.txt

ShouldBind.txt

I use ApiFox, modify Content-Type to "application/json", can't be mapping into the struct by two functions(ShouldBindJSON and ShouldBind)

Image

Struct, form and json setting.

type register struct {
	Username        string `form:"username" json:"username" `
	Useremail       string `form:"useremail" json:"useremail"`
	Passowrd        string `form:"passowrd" json:"password"`
	Confirmpassword string `form:"confirmpassword" json:"confirmpassword"`
}

func *RegisterUser()

func RegisterUser(c *gin.Context) {
	var reg register

	//c.Header("Content-Type", "application/json")
	e := c.ShouldBind(&reg)
	if e != nil {
		slog.Error("ERROR", "ERRORS", e)
	}
	slog.Info("INFO", "*RegisterUser() UserName", reg.Username, "UserEmail", reg.Useremail)
}

If the server uses ShouldBind, and the client requests to remove the application/json, it can access normally.

Using the Vue, JavaScript function *fetch() code to make a request, it still doesn't work.
"Content-Type": "application/json"

 const handleSubmit = ({values, errors}) => {
      console.log('values:', values, '\nerrors:', errors)
      // https://api.chingluennetwork.org
      $fetch('/api/v1/register/',{
        method: 'POST',
        body:{
          username:values.name,
          useremail:values.email,
          passowrd: values.password,
          confirmpassword: values.password2
        }
      })
    }

Sometimes it succeeds or fails.

2025/02/08 23:15:37 INFO INFO "*RegisterUser() UserName"=qwwqewq [email protected]
[GIN] 2025/02/08 - 23:15:37 | 200 |            0s |       127.0.0.1 | POST     "/api/v1/register/"

Request head

POST /api/v1/register/ HTTP/1.1
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
Connection: keep-alive
Content-Length: 96
Host: localhost:3000
Origin: http://localhost:3000
Referer: http://localhost:3000/oauth/register
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Mobile Safari/537.36
accept: application/json
content-type: application/json
sec-ch-ua: "Not A(Brand";v="8", "Chromium";v="132", "Google Chrome";v="132"
sec-ch-ua-mobile: ?1
sec-ch-ua-platform: "Android"

plz, plz, plz, ty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant