SoFunction
Updated on 2025-03-05

Golang gin framework to obtain parameters

1. Get URL parameters

GET request parameters are passed through URL

URL parameters can be obtained through the DefaultQuery() or Query() method.

DefaultQuery() If the parameter does not exist, return the default value. If the parameter does not exist, return the empty string.

user_id := (("user_id")).MustInt64()

page := (("page", "1")).MustInt()

2. Get form parameters/get Request body parameters

Place the POST parameter in the Request body

Form transmission is a post request, and the common transmission formats for http are four:

application/json
application/x-www-form-urlencoded
application/xml
multipart/form-data

The form parameters can be obtained through the PostForm() method, which defaults to parse parameters in x-www-form-urlencoded or from-data formats.

page := ("page")

rows := ("rows")

func (r *Request) PostFormValue(key string) string {
 if  == nil {
 (defaultMaxMemory)
 }
 if vs := [key]; len(vs) > 0 {
 return vs[0]
 }
 return ""
}
package controller
import (
 "bytes"
 "encoding/json"
 "/gin-gonic/gin"
)
func getRequestBody(context *, s interface{}) error { //Get request body body, _ := ("json") //Convert to json format reqBody, _ := body.(string)
 decoder := (([]byte(reqBody)))
 () //As a number instead of float64 err := (&s)//Storage parameters obtained from body into s return err
}
// Get post interface parametersfunc GetPostParams(ctx *) (map[string]interface{}, error) {
 params := make(map[string]interface{})
 err := getRequestBody(ctx, &params)
 return params, err
}

Use scenarios:

//Print the obtained parameterstype UpdatePassword struct {
 UserId int64 `json:"user_id"`
 LinkbookId string `json:"linkbook_id"`
 OldPassword string `json:"old_password"`
 NewPassword string `json:"new_password"`
}
func UpdateUserPassword(ctx *) {
 var updatePassword = UpdatePassword{}
 err := getRequestBody(ctx, &updatePassword)//The function encapsulated in the previous code block was called, and the one encapsulated by itself is not from the library if err != nil {
 (err)
 }
 ( )
 ( )
 ( )
 ( )
}

3. Get header parameters

Header is a key-value pair, which is easy to process. Tokens usually store headers.

Simple token, session Id, cookie id, etc.

// Get the content of the specified key in the header through the contextfunc GetHeaderByName(ctx *, key string) string {
 return (key)
}

Supplement: gin handles form to obtain parameters and mapping structures

Whether it is passing json or form value

Note that when defining the structure, the first letter must be capitalized.

//Define the structureId int form:"id"
Name string form:"name"
//Get and bind parametersid := (“id”)
var user User
(&user)
//Define the structureId int json:"id"
Name string json:"name"

Summarize:

As above: If it is a form passed value, the structure parameter is defined after the form, and the structure can be obtained, and the structure can also be bound; // If it is a form passed value, the structure parameter is defined after the structure parameter is defined after the json, and the structure can be obtained, but the structure cannot be bound;

If it is a json pass value, the parameter value cannot be taken, but the structure can be bound;

Get and bind parameters as above

Three binding methods:

() can be bound

() can be bound

ShouldBindQuery() can only bind get

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.