Bind Query String or Post Data

See the detail information.

  1. package main
  2. import "log"
  3. import "github.com/gin-gonic/gin"
  4. import "time"
  5. type Person struct {
  6. Name string `form:"name"`
  7. Address string `form:"address"`
  8. Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`
  9. }
  10. func main() {
  11. route := gin.Default()
  12. route.GET("/testing", startPage)
  13. route.Run(":8085")
  14. }
  15. func startPage(c *gin.Context) {
  16. var person Person
  17. // If `GET`, only `Form` binding engine (`query`) used.
  18. // If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`).
  19. // See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L48
  20. if c.ShouldBind(&person) == nil {
  21. log.Println(person.Name)
  22. log.Println(person.Address)
  23. log.Println(person.Birthday)
  24. }
  25. c.String(200, "Success")
  26. }

Test it with:

  1. $ curl -X GET "localhost:8085/testing?name=appleboy&address=xyz&birthday=1992-03-15"