Gin - Go lang如何使用Context.Request.Body并保留它? [英] Gin - Go lang How to use Context.Request.Body and retain it?
问题描述
我正在尝试编写一个中间件,我将对请求主体进行json模式验证。验证之后,我需要再次使用请求主体。但我无法弄清楚如何做到这一点。我提到了这篇文章
,并找到了访问该机构的方法。但是,一旦使用了请求体,我需要这个函数可用于我的下一个函数。
以下是示例代码:
包主
导入(
fmt
io / ioutil
净/ http
github.com/gin-gonic/gin
//\"github.com/xeipuuv/gojsonschema
)
func中间件()gin.HandlerFunc {
返回func(c * gin.Context){
//将在此执行json模式验证
body:= c.Request.Body
x,_: = ioutil.ReadAll(body)
fmt.Printf(%s \\\
,string(x))
fmt.Println(我是一个中间件JSON模式验证)
c.Next()
return
}
}
type E struct {
Email字符串
密码字符串
}
func测试(c * gin.Context){
//数据:=& E {}
//c.Bind(数据)
//fmt.Println(数据)//在json主体已被使用时打印为空
body := c.Request.Body
x,_:= ioutil.ReadAll(body)
fmt.Printf(body is:%s \\\
,string(x))
c.JSON(http.StatusOK,c)
}
func main(){
router:= gin.Default( )
router.Use(中间件())
router.POST(/ test,test)
//监听并发送
router.Run(127.0.0.1:8080)
}
当前输出:
{
email:test@test.com,
password:123
}
我是用于json模式验证的中间件
正文是:
预期输出:
{
email:test@test.com,
password:123
}
我是json模式的中间件验证
正文为:{
email:test@test.com,
password:123
}
<解决方案
你需要抓住并恢复身体。
Body是一个缓冲区,这意味着一旦你读了它,它就消失了。所以,如果你抓住它并放回去,你可以再次访问它。
检查这个答案,我认为这是你要找的:
https://stackoverflow.com/a/47295689/3521313
I am trying to write a middleware where I will be doing a json schema validation against the request body. After the validation, I need to use the request body again. But I am not able to figure out how this can be done. I referred this post and found a way to access the body. But once the request body is used, I need that available to my next function.
Here is the sample code:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/gin-gonic/gin"
//"github.com/xeipuuv/gojsonschema"
)
func middleware() gin.HandlerFunc {
return func(c *gin.Context) {
//Will be doing json schema validation here
body := c.Request.Body
x, _ := ioutil.ReadAll(body)
fmt.Printf("%s \n", string(x))
fmt.Println("I am a middleware for json schema validation")
c.Next()
return
}
}
type E struct {
Email string
Password string
}
func test(c *gin.Context) {
//data := &E{}
//c.Bind(data)
//fmt.Println(data) //prints empty as json body is already used
body := c.Request.Body
x, _ := ioutil.ReadAll(body)
fmt.Printf("body is: %s \n", string(x))
c.JSON(http.StatusOK, c)
}
func main() {
router := gin.Default()
router.Use(middleware())
router.POST("/test", test)
//Listen and serve
router.Run("127.0.0.1:8080")
}
Current output:
{ "email": "test@test.com", "password": "123" } I am a middleware for json schema validation body is:
Expected output:
{ "email": "test@test.com", "password": "123" } I am a middleware for json schema validation body is: { "email": "test@test.com", "password": "123" }
What Thellimist said, but in more words.
You need to "catch and restore" the Body. The Body is a buffer, that means that once you read it, its gone. So, if you catch it and "put it back", you can access it again.
Check this answer, I think this is what you are looking for: https://stackoverflow.com/a/47295689/3521313
这篇关于Gin - Go lang如何使用Context.Request.Body并保留它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!