“This is the 28th 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
- 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. The last article covered the log management module of the Gin framework. Today we will take a closer look at Gin’s model binding aspects.
The body of the
Many partners who interconnect with the server often have such questions: How does the server understand the request from the client? In fact, this requires us to define the API interface and message structure in advance. Among them, the API interface is very easy to understand, which is commonly referred to as message routing, such as /ping, /v1/upload, etc. So what is a message structure? Is the request parameter type we defined. This is what we are going to discuss today. In order to better parse these request parameters, Gin framework has designed a parameter model binding mechanism, which makes it very easy to parse the request parameters.
Currently, Gin framework supports four forms of model binding: JSON, XML, YAML, and standard formats (e.g., foo= bar&Boo =baz). When the server side needs to bind some form, you need to define it in advance. For example, if you want to parse the parameter name using JSON, you need to declare JSON :”name” in advance.
Here is an example of code:
// Binding from JSON
type Login struct {
User string `form:"user" json:"user" xml:"user" binding:"required"`
Password string `form:"password" json:"password" xml:"password" binding:"required"`
}
func main(a) {
router := gin.Default()
/ / bind JSON type parameters ({" user ":" manu ", "password" : "123"})
router.POST("/loginJSON".func(c *gin.Context) {
var json Login
iferr := c.ShouldBindJSON(&json); err ! =nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ifjson.User ! ="manu"|| json.Password ! ="123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})})// Example for binding XML (
/ /
// <root>
//
user
//
123
// </root>)
router.POST("/loginXML".func(c *gin.Context) {
var xml Login
iferr := c.ShouldBindXML(&xml); err ! =nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ifxml.User ! ="manu"|| xml.Password ! ="123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})})// Bind HTML arguments (user=manu&password=123)
router.POST("/loginForm".func(c *gin.Context) {
var form Login
// This will infer what binder to use depending on the content-type header.
iferr := c.ShouldBind(&form); err ! =nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ifform.User ! ="manu"|| form.Password ! ="123" {
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})})// Start the service and listen on port 8080
router.Run(": 8080")}Copy the code
Gin also provides two sets of binding methods for parametric model binding, MustBindWith and ShouldBindWith, one for binding mode and one for binding mode. The former triggers an error return when a parsing error occurs, while the latter requires the developer to handle the error himself. The relevant methods are as follows: Bind, BindJSON, BindXML, BindQuery, BindYAML, The binding modes are ShouldBind, ShouldBindJSON, ShouldBindXML, ShouldBindQuery, ShouldBindYAML.
At the end
Ok, so much for today’s content about Gin framework parsing parameter model binding mechanism. If you are interested, you can do it yourself, right? I hope today’s introduction will help you. See you tomorrow.
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!