[gogf/gf]r.GetRequestMap()返回内容包含form-data表单body信息

2024-06-25 305 views
8

request请求Content-Type为multipart/form-data时,返回r.GetRequestMap()获取数据会意外得到表单的body原始信息.

{
"----------------------------675405739396733188684112\r\nC ontent-Disposition:_form-data;_name": "\"passport\"\r\n\r\nxxx\r\n----------------------------675405739396733188684112\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\n123456\r\n----------------------------6754057 39396733188684112--\r\n",
"passport": "xxx",
"password": "123456"
}

原因猜测: request解析时parseBody[ghttp_requestparam.go:243]执行了r.bodyMap, = gstr.Parse(r.GetBodyString()) 将body按等号切割成了kv赋值给了bodyMap对象,GetRequestMap()[ghttp_request_param_request.go:97]获取时将r.xxxMap全部合并导致body原始信息出现在返回对象中.

回答

1

试了一下并不会,不确定是不是已经修复了 require github.com/gogf/gf/v2 v2.3.0

data := r.GetRequestMap()
glog.Debug(r.GetCtx(), data)
<body>
    <form action="/" method="post" encType="multipart/form-data">
        <input type="text" name="name" id="" value="123">
        <button type="submit">提交</button>
    </form>
</body>

image image

image

1

image 用postman测试的 没在网页端做个表单测试

3

@LonelySally 我先用postman试过,没出现,才写个页面测试。最好是能够提供版本,最小复现代码,否则不容易排查。 image image

0

找到原因了,你写一个中间件,在中间件的前置行为里执行一次r.GetBody() func (s *sMiddleware) Test(r *ghttp.Request) { r.GetBody() r.Middleware.Next() } 然后在控制器里执行g.Dump(g.RequestFromCtx(ctx).GetRequestMap())就有输出了

3

4698fda1c920f01607c78bb0a0a556b 准确定位到,因为先r.GetBody(),会保留body内容,那么body内容将会被parse进bodymap,而如果先GetRequestMap(),在GetRequestMap()里面包含了原生的ParseForm,ParseForm会清空r.body内容,而导致r.GetBody()为空。