package main
import (
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
func main() {
r := gin.Default()
r.POST("/test", func(c *gin.Context) {
bodyBytes, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
c.Abort()
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
name := c.PostForm("name")
c.String(http.StatusOK, "%s, %s", name, string(bodyBytes))
})
err := r.Run()
if err != nil {
fmt.Println(err)
}
}curl -X POST "http://127.0.0.1:8080/test" -d "name=zqunor"
虽然能实现,但是感觉回传不是个好方案。
获取body内容