Created
April 29, 2021 14:32
-
-
Save thiagozs/4ece9de7ce889bcaadb5b0175f240b6c to your computer and use it in GitHub Desktop.
Gin bind query form params e json data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
import "github.com/gin-gonic/gin" | |
type Person struct { | |
Name string `form:"name" json:"name"` | |
Address string `form:"address" json:"address"` | |
} | |
func main() { | |
route := gin.Default() | |
route.GET("/testing", startPage) | |
route.Run(":8085") | |
} | |
func startPage(c *gin.Context) { | |
var person Person | |
if c.Bind(&person) == nil { | |
log.Println("====== Bind By Query String ======") | |
log.Println(person.Name) | |
log.Println(person.Address) | |
} | |
if c.BindJSON(&person) == nil { | |
log.Println("====== Bind By JSON ======") | |
log.Println(person.Name) | |
log.Println(person.Address) | |
} | |
c.String(200, "Success") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bind by query
$ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz"
bind by json