Bind query string or post data

See the detail information.

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

Test it with:

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

Last modified March 7, 2020 : add blog dir (#115) (f46734b)