The front-end of the Gin framework transmits data in json format, corresponding to the back-end structure

1. The front-end of the Gin framework transmits data in json format, corresponding to the back-end structure (similar to javaJson format for passing and receiving parameters)

insert image description here
Backend corresponding structure:
insert image description here

The technology used by Gin, the .BindJSON() method

It means that the json data sent by the binding front end is converted into the corresponding structure of the back end

package main

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

func main() {
    
    
	r := gin.Default()
	type LoginForm struct {
    
    
		Name     string `form:"name" binding:"required"`
		Password string `form:"password" binding:"required"`
	}
	r.POST("/login", func(c *gin.Context) {
    
    
		json := LoginForm{
    
    }
		//将前端json格式数据与LoginForm对象绑定
		c.BindJSON(&json)
		//将结构体已json格式响应回前端
		c.JSON(200, json)
	})
	r.Run(":8080") // 监听并在 0.0.0.0:8080 上启动服务
}

result

insert image description here

Guess you like

Origin blog.csdn.net/qq_44798321/article/details/129347470