以下内容转载自 https://blog.youkuaiyun.com/weixin_36344862/article/details/111932206
如题,post发送数据有几种形式,form和流是最常用的。特别是在程序里使用httpc
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.POST("/events", events)
router.Run(":5000")
}
func events(c *gin.Context) {
buf := make([]byte, 1024)
n, _ := c.Request.Body.Read(buf)
fmt.Println(string(buf[0:n]))
resp := map[string]string{"hello": "world"}
c.JSON(http.StatusOK, resp)
/*post_gwid := c.PostForm("name")
fmt.Println(post_gwid)*/
}
lients,一般都算通过流发送。在php里,是通过php://input来获取的。在gin中,可以通过c.Request.Body.Read(buf)。具体代码如下:
该博客介绍了在Gin框架下处理POST请求时,如何读取和解析通过流形式发送的数据。示例代码展示了如何使用`c.Request.Body.Read(buf)`从请求体中获取数据,并提供了PHP中通过`php://input`获取相同数据的对比。此外,还提及了在 Gin 中使用`c.PostForm`获取表单数据的方法。
6万+

被折叠的 条评论
为什么被折叠?



