Problems with parameter injection in gin

Questions about parameter injection

If during development, small parameters are not injected into the structure according to the established requirements, at this time, we must look at the request method? If it is a post request,

Front-end—post—json{id:1,pageSize:10,page:1}————————————

Parameter injection method: ShouldBindJSON

  • post + json

  • Pass the json parameters, post request

    • {page:1,pageSize:10,keyword:“”}
  • gin/beego —context—-c.Request.Body

    • [1,2,3,2,33,4,5,55,55,33]
  • json library method json.NewDecoder®—Decoder object

    • Inject into pageInfo

    • type PageInfo struct {
              
              
         Page     int    `json:"page" form:"page"`         // 页码
         PageSize int    `json:"pageSize" form:"pageSize"` // 每页大小
         Keyword  string `json:"keyword" form:"keyword"`   //关键字
      }
      
  • Decoder.Decode(pageInfo)

    • The bottom layer is to find the attributes of all structures through reflection, find the json tag through the attributes, check whether the name is consistent with the jsonkey, and if so, directly assign the value to the attribute.
  • Finish.

Parameter injection method: ShouldBindQuery

  • ​ post + query ?id=1&name=1
  • ​ get + query ?id=1&name=1

Guess you like

Origin blog.csdn.net/picktheshy/article/details/132357020