“form”是一个可以在解析URL查询参数到结构体时使用的合适的结构标签吗?

huangapple go评论75阅读模式
英文:

Is 'form' an acceptable struct tag to use when parsing URL query parameters into a struct?

问题

我有一个GET请求函数,使用gin.Context接收查询参数。我目前使用context.ShouldBindQuery(&myQueryParamStruct)将这些查询参数传递到一个结构体中,结构体如下所示:

type myParams struct {
    Color string `form:"color"`
    Size  int    `form:"size"`
}

我的问题是,在这种情况下使用form标签会出现问题吗(因为我实际上并没有使用表单,而是使用查询参数),是否有更好的结构体标签来实现这个目的?

英文:

I've got a GET request function that takes in query parameters using gin.Context. I'm currently passing those query parameters using context.ShouldBindQuery(&myQueryParamStruct) into a struct that looks like

type myParams struct {
    Color string `form:"color"`
    Size  int    `form:"size"`
}

My question is will issues arise when using the form tag to do this (since I'm not technically using a form, I'm using query parameters), and is there a better struct tag for this purpose?

答案1

得分: 1

form是正确的标签,你不需要担心。

还要注意的是,使用GET请求发送的HTML表单是通过查询字符串而不是请求体发送的。此外,查询字符串和表单体都采用了application/x-www-form-urlencoded的格式。

英文:

form is the correct tag, you needn't worry.

Note also that html forms sent using the GET request are sent in the query string rather than the body. Furthermore, query strings and form bodies both have the application/x-www-form-urlencoded format.

huangapple
  • 本文由 发表于 2021年7月28日 07:06:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/68552641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定