gin c.Request.PostForm为空问题原理已经解决方法

本文最后更新于:2021年9月20日 上午

其实这个还是蛮简单的,初学时犯的错误,当时还是不太理解gin框架的整体流程

问题的发现:

1
2
3
engine.POST("/login", func(c *gin.Context) {
c.JSON(http.StatusOK, c.Request.PostForm)
})

请求、响应报文(报文都略有删减)如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 请求报文
POST /login?userid=123 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 31
Connection: keep-alive

username=user&password=password


// 响应报文
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 5

null

按理来说我们应该接收这样的报文:

1
2
3
4
5
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 46

{"password":["password"],"username":["user"]}

我其实挺疑惑的,接着我通过message := c.PostForm("message")获取参数打印出来,代码如下:

1
2
3
4
5
engine.POST("/login", func(c *gin.Context) {
fmt.Println(c.PostForm("username"))
fmt.Println(c.PostForm("password"))
c.JSON(http.StatusOK, c.Request.PostForm)
})

然后,就成功了??????报文很正常的打印出了我们想要的结果,(黑人问号.jpg)
很自然的我们就想到了c.PostForm("")的副作用"解析"了c.Request.PostForm,使得我们使用时有数据,我们来验证一下:

1
2
3
4
5
6
7
8
9
10
11
engine.POST("/login", func(c *gin.Context) {
fmt.Println(c.Request.PostForm)
fmt.Println(c.PostForm("username"))
fmt.Println(c.Request.PostForm)
c.JSON(http.StatusOK, c.Request.PostForm)
})
/*
map[]
user
map[password:[password] username:[user]]
*/

结果显示确实如此,这条c.PostForm语句仿佛有魔力一样"解析了"报文,使得c.Request.PostForm里面填充了数据,实际上c.Request.FormValue()也有这种魔力。我们拨开迷雾去寻找真相吧。我们首先分析c.PostForm这个方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// PostForm是通过c.GetPostForm拿到数据,忽略第二个返回值
func (c *Context) PostForm(key string) string {
value, _ := c.GetPostForm(key)
return value
}
// 可以看到GetPostForm也是通过GetPostFormArray拿到返回值,但是只关注第一个值
func (c *Context) GetPostForm(key string) (string, bool) {
if values, ok := c.GetPostFormArray(key); ok {
return values[0], ok
}
return "", false
}
// 这个方法作用很明显,获取formCache,从fromCache中拿到key对应的value,如果没有就返回一个空值,那么副作用肯定出现在c.getFormCache()中
func (c *Context) GetPostFormArray(key string) ([]string, bool) {
c.getFormCache()
if values := c.formCache[key]; len(values) > 0 {
return values, true
}
return []string{}, false
}
// 这个方法也很简单,只有ParseMultipartForm能产生副作用,其它语句都不可能,就不分析了
func (c *Context) getFormCache() {
if c.formCache == nil {
c.formCache = make(url.Values)
req := c.Request
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
if err != http.ErrNotMultipart {
debugPrint("error on parse multipart form array: %v", err)
}
}
c.formCache = req.PostForm
}
}
// 代码很长没有贴全,通过命名我们得到了关键的语句:r.ParseForm,可以去测试一下是否是这条语句
func (r *Request) ParseMultipartForm(maxMemory int64) error {
if r.MultipartForm == multipartByReader {
return errors.New("http: multipart handled by MultipartReader")
}
if r.Form == nil {
err := r.ParseForm()
if err != nil {
return err
}
}
...
}
// c.Request.FormValue()也是通过ParseForm去解析的,同时parseForm的代码就不分析了

最后让我们测试一下:

1
2
3
4
5
6
7
8
9
10
11
engine.POST("/login", func(c *gin.Context) {
fmt.Println(c.Request.PostForm)
_ = c.Request.ParseForm()
fmt.Println(c.Request.PostForm)
c.JSON(http.StatusOK, c.Request.PostForm)
})
/*
map[]
user
map[password:[password] username:[user]]
*/

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!