在使用gin框架的时候,发现请求的body数据只允许读取一次,针对这种情况有2种解决办法
第一步:base code:
package main
import (
"fmt"
"gopkg.in/gin-gonic/gin.v1"
"net/http"
"io/ioutil"
"bytes"
//"encoding/json"
)
type Person struct{
Name string `json:"name"`
Phone int64 `json:"phone"`
Data string `json:"data"`
}
func main(){
router := gin.Default()
router.POST("/",Hello)
router.Run(":8000")
}
func Hello(ctx *gin.Context){
var info Person
err := ctx.BindJSON(&info)
if err != nil{
fmt.Println(err.Error())
}
fmt.Printf("info: %#v\n",info)
ctx.JSON(http.StatusOK, gin.H{
"code":200,
"msg":"success",
})
}
随便一组请求数据,得到结果:
[GIN-debug] Listening and serving HTTP on :8000
info: main.Person{Name:"lisa", Phone:13700001234, Data:"thisIsTest"}
这个是一般情况下使用场景,如果需要加一个中间件【验证身份…】之类的
第二步:加一个中间件供能
package main
import (
"fmt"
"gopkg.in/gin-gonic/gin.v1"
"net/http"
"io/ioutil"
"bytes"
//"encoding/json"
)
type Person struct{
Name string `json:"name"`
Phone int64 `json:"phone"`
Data string `json:"data"`
}
func main(){
router := gin.Default()
router.POST("/",HelloMiddleware(),Hello)
router.Run(":8000")
}
func HelloMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
data,err := ctx.GetRawData()
if err != nil{
fmt.Println(err.Error())
}
fmt.Printf("data: %v\n",string(data))
ctx.Next()
}
}
func Hello(ctx *gin.Context){
var info Person
err := ctx.BindJSON(&info)
if err != nil{
fmt.Println(err.Error())
}
fmt.Printf("info: %#v\n",info)
ctx.JSON(http.StatusOK, gin.H{
"code":200,
"msg":"success",
})
}
此时运行结果:
[GIN-debug] Listening and serving HTTP on :8000
data: {"name":"lisa","phone":13700001234,"data":"thisIsTest"}
EOF
info: main.Person{Name:"", Phone:0, Data:""}
可以看出,BindJSON这一步报错:EOF,而先于Hello函数运行的中间件正常打印出读取的data数据,而info仅仅是默认值信息
解决这个问题的方法有2个:
第一种:利用gin自带函数Set
package main
import (
"fmt"
"gopkg.in/gin-gonic/gin.v1"
"net/http"
//"io/ioutil"
//"bytes"
"encoding/json"
)
type Person struct{
Name string `json:"name"`
Phone int64 `json:"phone"`
Data string `json:"data"`
}
func main(){
router := gin.Default()
router.POST("/",HelloMiddleware(),Hello)
router.Run(":8000")
}
func HelloMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
data,err := ctx.GetRawData()
if err != nil{
fmt.Println(err.Error())
}
fmt.Printf("data: %v\n",string(data))
var info Person
json.Unmarshal(data,&info)
ctx.Set("name",info.Name)
ctx.Set("phone",info.Phone)
ctx.Set("data",info.Data)
ctx.Next()
}
}
func Hello(ctx *gin.Context){
name,_ := ctx.Get("name")
phone,_ := ctx.Get("phone")
data,_ := ctx.Get("data")
info := Person{
Name:name.(string),
Phone:phone.(int64),
Data:data.(string),
}
fmt.Printf("info: %#v\n",info)
}
运行结果【PS:这才是想要的结果】:
[GIN-debug] Listening and serving HTTP on :8000
data: {"name":"lisa","phone":13700001234,"data":"thisIsTest"}
info: main.Person{Name:"lisa", Phone:13700001234, Data:"thisIsTest"}
第二种:利用golang官方库,推荐这种方法解决这个问题
package main
import (
"fmt"
"gopkg.in/gin-gonic/gin.v1"
"net/http"
"io/ioutil"
"bytes"
"encoding/json"
)
type Person struct{
Name string `json:"name"`
Phone int64 `json:"phone"`
Data string `json:"data"`
}
func main(){
router := gin.Default()
router.POST("/",HelloMiddleware(),Hello)
router.Run(":8000")
}
func HelloMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
data,err := ctx.GetRawData()
if err != nil{
fmt.Println(err.Error())
}
fmt.Printf("data: %v\n",string(data))
ctx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data)) // 关键点
ctx.Next()
}
}
func Hello(ctx *gin.Context){
var info Person
err := ctx.BindJSON(&info)
if err != nil{
fmt.Println(err.Error())
}
fmt.Printf("info: %#v\n",info)
ctx.JSON(http.StatusOK, gin.H{
"code":200,
"msg":"success",
})
}
运行结果【PS:有没有跟上面一样】:
[GIN-debug] Listening and serving HTTP on :8000
data: {"name":"lisa","phone":13700001234,"data":"thisIsTest"}
info: main.Person{Name:"lisa", Phone:13700001234, Data:"thisIsTest"}