Request Parameters

Overview

In go-zero,it can get request parameters from http.Request,which is the smae way to get Form from http.Request, where Body only accepts request parameters in application/json format, and Form only accepts request parameters in application/x-www-form-urlencoded format. In addition to that, go-zero also supports path parameters and request header parameters fetching, both of which are parsed into structures via the httpx.Parse method.

Parameter Usage

Form request parameters

For form request parameters, go-nero supports defining the name of the parameter by form tag, which supports Content-Type for application/x-www-form-urlencoded , Query arguments that support the Get method, written to add form keywords to the tag of the architecture, The writing in tag is a k-v structure, key is a fixed value form, value is a key value for query.

  1. type Request struct {
  2. Name string `form:"name"` // required
  3. Age int `form:"age,optional"` // optional
  4. }
  5. var req Request
  6. err := httpx.Parse(r, &req)

Json Request Parameters

对于 Json 参数请求,其接受来自 Post 请求方法 Content-Type 为 application/json 的请求参数,其写法是在结构体的 tag 中加上 json 关键字, 在 tag 中写法是一个 k-v 结构,key 是固定值 json,value 是对应 json 的 key 值。

  1. type Request struct {
  2. Name string `json:"name"`
  3. Age int `json:"age"`
  4. }
  5. var req Request
  6. err := httpx.Parse(r, &req)

Path Request Parameters

For the Path parameter request, it supports the definition of parameters on the path by adding path keywords to the tag of the architecture, used to get a particular path parameter, a k-v format, key is a fixed value path, value is a path argument.

  1. type Request struct {
  2. Name string `path:"name"`
  3. }
  4. // Path definitions
  5. rest.Route{
  6. Method: http.MethodGet,
  7. Path: "/user/:name",
  8. Handler: handle,
  9. }
  10. var req Request
  11. err := httpx.Parse(r, &req)

Header parameters get

Header parameter gets, written to add header keywords to the tag of the architecture, which is a k-v format, key is a fixed value header, value is the key value of the request header.

  1. type Request struct {
  2. Authorization string `header:"authorization"`
  3. }
  4. var req Request
  5. err := httpx.Parse(r, &req)

Parameter Validation

Parameter optional

In go-zero, the parameter is optional via optional keywords in a structural tag. If the parameter is not required you can add optional keywords after the parameter otherwise it is considered required

  1. type Request struct {
  2. Age int `form:"age, optional"`
  3. }
  4. var req Request
  5. err := httpx.Parse(r, &req)
Request Parameters - 图1tip

For required parameters, if no pass is made, the following error will be returned.

  1. field age is not set

Parameter range definition

go-zero provides the definition of parameter intervals, which can be defined with the range keyword,It is written by adding the range keyword to the tag of the structure,It is written in the format range=$range_expression.

  1. type Request struct {
  2. Age int `form:"age,range=[18:35)"`
  3. }
  4. var req Request
  5. err := httpx.Parse(r, &req)

See api syntax for more details on parameter intervals parameter rules

Parameter enumeration value

go-Zero provides a definition of parameter enumeration values, which can be defined by options keywords by adding options to a structure's tag keyword in the format options=$option_expression.The enumeration is divided by comma in English, e.g. options=18,19meaning that only 18 and 19 values are accepted, but also illegal.

  1. type Request struct {
  2. Age int `form:"age,options=18,19"`
  3. }
  4. var req Request
  5. err := httpx.Parse(r, &req)

Default value for parameters

go-zero provides a default value for the parameters. The default value of the parameter can be defined by default keyword by adding default keyword to a structural tag in the format default=$default_valuewhose function is an extension of optional parameters, optional parameter default value is zero.

  1. type Request struct {
  2. Age int `form:"age,default=18"`
  3. }
  4. var req Request
  5. err := httpx.Parse(r, &req)

References