当前位置:网站首页>Go: Gin urlencoded format

Go: Gin urlencoded format

2022-07-23 15:23:00 It workers

Code :

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    router := gin.Default()

    router.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,
        })
    })
    router.Run(":8080")
}

Run the program

Client side test

->curl  localhost:8080/form_post --form message=hello
{"message":"hello","nick":"anonymous","status":"posted"}

Server log

原网站

版权声明
本文为[It workers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231018568755.html