🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ type LoginForm struct { User string `form:"user" binding:"required"` Password string `form:"password" binding:"required"` } func main() { r := gin.Default() r.POST("/login", func(c *gin.Context) { var form LoginForm if c.ShouldBind(&form) == nil { if form.User == "user" && form.Password == "password" { c.JSON(200, gin.H{"status": "login successfully!"}) } else { c.JSON(401, gin.H{"status": "login failed!"}) } } }) r.Run(":8888") } ~~~ ~~~ func main() { r := gin.Default() r.POST("/form_post", func(c *gin.Context) { message := c.PostForm("message") nick := c.DefaultPostForm("nick", "anonymous") c.JSON(200, gin.H{ "status": "posted", "message": message, "nick": nick, }) }) r.Run(":8888") } ~~~