Only bind query string

ShouldBindQuery function only binds the query params and not the post data. See the detail information.

  1. package main
  2. import (
  3. "log"
  4. "github.com/gin-gonic/gin"
  5. )
  6. type Person struct {
  7. Name string `form:"name"`
  8. Address string `form:"address"`
  9. }
  10. func main() {
  11. route := gin.Default()
  12. route.Any("/testing", startPage)
  13. route.Run(":8085")
  14. }
  15. func startPage(c *gin.Context) {
  16. var person Person
  17. if c.ShouldBindQuery(&person) == nil {
  18. log.Println("====== Only Bind By Query String ======")
  19. log.Println(person.Name)
  20. log.Println(person.Address)
  21. }
  22. c.String(200, "Success")
  23. }

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