“This is the 15th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

directory

  • preface

  • The body of the

    • Query parameter parsing
    • Multipart/Urlencoded Form parameter parsing
    • Query + POST form parameter parsing
    • Map Parameter Parsing
  • At the end

preface

Gin is an HTTP Web framework implemented using the pure Golang language. Gin is now widely used for its simple interface design and high performance. Today, we’ll take a closer look at how Gin performs parameter parsing.

The body of the

Query parameter parsing

The Query parameter is the most common of all requests, as shown in the following code:

package main 

import "github.com/gin-gonic/gin"

func main(a) {
	router := gin.Default()
        
	router.GET("/welcome".func(c *gin.Context) {
		firstname := c.DefaultQuery("firstname"."Guest")
		lastname := c.Query("lastname") 

		c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
	})
	router.Run(": 8080")}Copy the code

The above API can match urls with requests like the following:

/welcome? firstname=Jane&lastname=Doe

If firstName is missing in the Query part of the above Get request, the value of firstname will be assigned to “Guest”; However, if the parameter lastname is missing, the value of lastname is null.

Multipart/Urlencoded Form parameter parsing

In all requests, there are also Multipart/Urlencoded Form parameters, which can be understood in the following code:

package main 

import "github.com/gin-gonic/gin"

func main(a) {
	router := gin.Default()

	router.POST("/form_post".func(c *gin.Context) {
		message := c.PostForm("message")
		nick := c.DefaultPostForm("nick"."anonymous")

		c.JSON(200, gin.H{
			"status":  "posted"."message": message,
			"nick":    nick,
		})
	})
	router.Run(": 8080")}Copy the code

In the Post request above, if the Body part is missing the parameter Nick, the value of Nick will be assigned to “anonymous”; However, if the parameter message is missing, the value of message is null.

Query + POST form parameter parsing

Some requests may have both query and POST form parameters, for example:

POST /post? Id = 1234 & page = 1 HTTP / 1.1

Content-Type: application/x-www-form-urlencoded

name=manu&message=this_is_great

Example code:

package main 

import "github.com/gin-gonic/gin"

func main(a) {
	router := gin.Default()

	router.POST("/post".func(c *gin.Context) {

		id := c.Query("id")
		page := c.DefaultQuery("page"."0")
		name := c.PostForm("name")
		message := c.PostForm("message")

		fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message)
	})
	router.Run(": 8080")}Copy the code

As you can see from the above example, the Query and DefaultQuery methods are used when parsing a Query parameter. When parsing parameters of the PostForm type, the PostForm method is used.

Map Parameter Parsing

Another type of request parameter is Map. An example of a request is as follows:

POST /post? Ids [a] = 1234 & ids [b] = hello HTTP / 1.1

Content-Type: application/x-www-form-urlencoded

names[first]=thinkerou&names[second]=tianou

How do you resolve this situation? See the code below:

func main(a) {
	router := gin.Default()

	router.POST("/post".func(c *gin.Context) {

		ids := c.QueryMap("ids")
		names := c.PostFormMap("names")

		fmt.Printf("ids: %v; names: %v", ids, names)
	})
	router.Run(": 8080")}Copy the code

The print result is as follows:

ids: map[b:hello a:1234], names: map[second:tianou first:thinkerou]

At the end

Ok, so today I’ve shown you how to use Gin framework to resolve four different parameter requests. I hope it will be helpful for you to learn how to use the Gin framework.

About the author: Hello, everyone, I am Liuzhen007, an audio and video technology fan, CSDN blog expert, Huawei Cloud community cloud sharing expert, signed the author, welcome to follow me to share more dry products!