mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 01:57:55 +08:00
Model binding and validation be more readable. #1484
This commit is contained in:
parent
5acaa644f6
commit
4c6626aeea
48
README.md
48
README.md
@ -557,15 +557,17 @@ func main() {
|
|||||||
// Example for binding JSON ({"user": "manu", "password": "123"})
|
// Example for binding JSON ({"user": "manu", "password": "123"})
|
||||||
router.POST("/loginJSON", func(c *gin.Context) {
|
router.POST("/loginJSON", func(c *gin.Context) {
|
||||||
var json Login
|
var json Login
|
||||||
if err := c.ShouldBindJSON(&json); err == nil {
|
if err := c.ShouldBindXML(&json); err != nil {
|
||||||
if json.User == "manu" && json.Password == "123" {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if json.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 (
|
// Example for binding XML (
|
||||||
@ -576,30 +578,34 @@ func main() {
|
|||||||
// </root>)
|
// </root>)
|
||||||
router.POST("/loginXML", func(c *gin.Context) {
|
router.POST("/loginXML", func(c *gin.Context) {
|
||||||
var xml Login
|
var xml Login
|
||||||
if err := c.ShouldBindXML(&xml); err == nil {
|
if err := c.ShouldBindXML(&xml); err != nil {
|
||||||
if xml.User == "manu" && xml.Password == "123" {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if xml.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"})
|
||||||
})
|
})
|
||||||
|
|
||||||
// Example for binding a HTML form (user=manu&password=123)
|
// Example for binding a HTML form (user=manu&password=123)
|
||||||
router.POST("/loginForm", func(c *gin.Context) {
|
router.POST("/loginForm", func(c *gin.Context) {
|
||||||
var form Login
|
var form Login
|
||||||
// This will infer what binder to use depending on the content-type header.
|
// This will infer what binder to use depending on the content-type header.
|
||||||
if err := c.ShouldBind(&form); err == nil {
|
if err := c.ShouldBind(&form); err != nil {
|
||||||
if form.User == "manu" && form.Password == "123" {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if form.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"})
|
||||||
})
|
})
|
||||||
|
|
||||||
// Listen and serve on 0.0.0.0:8080
|
// Listen and serve on 0.0.0.0:8080
|
||||||
|
Loading…
x
Reference in New Issue
Block a user