Skip to main content

Normal Request Input

The basic code is as follows: after binding the route to the controller using group, write the following method in the controller. All the following code is modified here:

func (c *Controller) Params(request *ghttp.Request) {
m := request.GetQueryMap()
request.Response.WriteJson(m)
}

Retrieving Query Parameters

Query parameters are parameters written in the URL in the form of ?a=1&b=2, usually passed via the GET method.

Single Parameter Value

m := request.GetQuery("name")

GetQuery retrieves the value of the specified parameter name. If the value does not exist, it returns nil.

You can also specify a default value, which will be returned if the parameter value does not exist:

m := request.GetQuery("name", "孙行者")

The returned type is gvar.Var, which can be type-cast as needed. Common type conversion methods include:

func (v *Var) Bytes() []byte
func (v *Var) String() string
func (v *Var) Bool() bool
func (v *Var) Int() int
func (v *Var) Int8() int8
func (v *Var) Int16() int16
func (v *Var) Int32() int32
func (v *Var) Int64() int64
func (v *Var) Uint() uint
func (v *Var) Uint8() uint8
func (v *Var) Uint16() uint16
func (v *Var) Uint32() uint32
func (v *Var) Uint64() uint64
func (v *Var) Float32() float32
func (v *Var) Float64() float64
func (v *Var) Time(format ...string) time.Time
func (v *Var) Duration() time.Duration
func (v *Var) GTime(format ...string)

Batch Retrieval of Query Parameters

GoFrame provides three methods GetQueryMap, GetQueryMapStrStr, GetQueryMapStrVar for batch retrieval of Query parameters. They have the same usage but return different types.

  • Retrieving all Query parameters
m := request.GetQueryMap()
  • Specifying parameter names and default values
m := request.GetQueryMap(map[string]interface{}{"name": "者行孙", "age": 600})

Converting Query Parameters to Custom Struct

You can define a custom struct and directly convert the request parameters to corresponding struct fields:

type user struct {
Name string
Age int
}
var u *user
err := request.ParseQuery(&u)
if err != nil {
request.Response.WritelnExit("转换出借")
}

In the example above, if the struct fields Name and Age match the parameter names name and age, the conversion succeeds. If they don't match, you can specify the parameter names using tags like json:, param:, or p: as follows:

type user struct {
UserName string `json:"name"`
UserAge int `p:"age"`
}

Retrieving Form Parameters (POST Parameters)

Retrieving form parameters refers to getting data submitted via application/x-www-form-urlencoded, application/form-data, multipart/form-data, and also JSON format data submitted via POST method.

Single Parameter

m := request.GetForm("name")

GetForm retrieves the value of the specified form parameter name. If the parameter does not exist, it returns nil.

You can specify a default value to be returned if the parameter does not exist:

m := request.GetForm("name", "烧包谷")

The returned type is gvar.Var, which can be type-cast as needed.

Batch Retrieval of Request Data

You can use GetFormMap, GetFormMapStrStr, GetFormMapStrVar to batch retrieve request data. They have the same usage but return different types.

m := request.GetFormMap()

You can specify parameters to retrieve and their default values:

m := request.GetFormMap(map[string]interface{}{"name": "大洋芋"})

Converting Request Data to Custom Struct

Similar to Query parameters, you can directly convert request parameters to a custom struct. If the struct field names do not match the parameter names, you can use tags like json:, param:, p: to specify the corresponding parameter names:

type user struct {
UserName string `json:"name"`
UserAge int `p:"age"`
}
var u *user
err := request.ParseForm(&u)
if err != nil {
request.Response.WritelnExit("转换出借")
}

Retrieving Dynamic Route Parameters

To retrieve dynamic route parameters, you need to make a slight modification to existing code. Define request and response data formats in the api package, and dynamically register specified routes:

api

package api

import (
"github.com/gogf/gf/v2/frame/g"
)

type Res struct {
g.Meta `mime:"text/html"`
}

type ParamReq struct {
g.Meta `path:"/params/:name" method:"all"`
}

Modify the controller method using the api data structure:

Controller

func (c *Controller) Params(ctx context.Context, req *api.ParamReq) (res *api.Res, err error) {
request := g.RequestFromCtx(ctx)
u := request.GetRouter("name")
request.Response.WriteJson(g.Map{"data": u})
return
}

Retrieve Single Parameter

u := request.GetRouter("name")

It returns a gvar.Var type, which can be type-cast as needed. You can also specify a default value.

Batch Retrieval of Parameters

u := request.GetRouterMap()

It returns a map[string]string. If dynamic routing is not set, it returns nil.

Retrieving All Request Parameters

GoFrame also provides methods to retrieve all request parameters, similar to the above two methods, but without distinguishing request methods. If the same parameter name exists in both GET and POST requests, the POST parameter takes precedence.

Retrieve Single Parameter

data := request.GetRequest("name")
// Shortened form
data := request.Get("name")

It returns a gvar.Var type, and you can provide a default value.

Batch Retrieval of Request Parameters

data := request.GetRequestMap()
// You can specify parameters to retrieve and their default values
data := request.GetRequestMap(g.Map{"name": ""})
// There are also the following variations
data := request.GetRequestMapStrStr()
data := request.GetRequestMapStrVar()

Convert Request Parameters to Custom Struct

request.Parse(&u) // u is a pointer to a custom struct