router.GET("/user/:name", func(c *gin.Context) { name := c.Param("name") c.JSON(http.StatusOK,gin.H{"name":name}) })
router.GET("/params", func(context *gin.Context) { firstname := context.DefaultQuery("firstname","defaultName") lastname := context.Query("lastname") context.JSON(http.StatusOK,gin.H{ "firstName":firstname, "lastName":lastname, }) })
router.POST("/form_post", func(c *gin.Context) { name := c.PostForm("name") nick := c.DefaultPostForm("age", "90") c.JSON(200, gin.H{ "name": name, "age": nick, }) })
type User struct {json
Username string `form:"username" json:"username" binding:"required"` Passwd string `form:"passwd" json:"passwd" bdinding:"required"` Age int `form:"age" json:"age"`
}app
router.POST("/json", func(context *gin.Context) { var user User context.BindJSON(&user) context.JSON(200,gin.H{"data":user}) })