如何在Go中接收使用multipart/form-data边界的POST参数

huangapple go评论112阅读模式
英文:

How to receive POSTed parameter with multipart/form-data boundary in Go

问题

我正在使用HayaGeek的jQuery文件上传插件,并成功发送了一个请求,可以在Chrome的开发者工具中看到:

/* 通用 */
远程地址:127.0.0.1:80
请求URL:http://127.0.0.1/profile/edit
请求方法:POST
状态码:200 OK

/* 响应头 */
连接:保持连接
内容长度:101
内容类型:application/json
日期:Fri, 24 Apr 2015 02:04:51 GMT
服务器:nginx/1.6.2
设置Cookie:SK=A; 路径=/; 过期时间:Fri, 24 Apr 2015 04:04:51 UTC

/* 请求头 */
接受:*/*
接受编码:gzip, deflate
接受语言:en-US,en;q=0.8,id;q=0.6
连接:保持连接
内容长度:12855
内容类型:multipart/form-data; boundary=----WebKitFormBoundarydlN4BAQWeDyF512h
Cookie:SK=A
主机:127.0.0.1
来源:http://127.0.0.1
引用者:http://127.0.0.1/profile/edit
用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
X-Requested-With:XMLHttpRequest

/* 请求有效载荷 */
------WebKitFormBoundarydlN4BAQWeDyF512h
内容分配:form-data; name="file"; filename="screen-2015-04-23-21-01-51.png"
内容类型:image/png


------WebKitFormBoundarydlN4BAQWeDyF512h
内容分配:form-data; name="a"

file_upload
------WebKitFormBoundarydlN4BAQWeDyF512h
内容分配:form-data; name="key"

transcript
------WebKitFormBoundarydlN4BAQWeDyF512h
内容分配:form-data; name="id"

379
------WebKitFormBoundarydlN4BAQWeDyF512h
内容分配:form-data; name=""

undefined
------WebKitFormBoundarydlN4BAQWeDyF512h--

获取上传的file没有问题,可以使用以下命令:

file, header, err := request.FormFile(`file`) 
// `request` 是 `*http.Request`

但是获取其余的POST参数时遇到了问题,我尝试了以下方法但没有成功:

 a := request.FormValue(`a`) // 应该是:`file_upload`
 id := request.FormValue(`id`) // 应该是:`transcript`
 key := request.FormValue(`key`) // 应该是:`379`
 // 通常情况下这样是可以的,但对于这个插件却不起作用

一个普通的POST请求(没有boundary,没有该插件)会得到类似以下的结果,可以使用上述命令正常检索到POST的参数:

/* 通用 */
远程地址:127.0.0.1:80
请求URL:http://127.0.0.1/profile/edit
请求方法:POST
状态码:200 OK

/* 响应头 */
连接:保持连接
内容长度:297
内容类型:application/json
日期:Fri, 24 Apr 2015 02:17:31 GMT
服务器:nginx/1.6.2
设置Cookie:SK=A; 路径=/; 过期时间:Fri, 24 Apr 2015 04:17:31 UTC

- 请求头
接受:*/*
接受编码:gzip, deflate
接受语言:en-US,en;q=0.8,id;q=0.6
连接:保持连接
内容长度:23
内容类型:application/x-www-form-urlencoded; charset=UTF-8
Cookie:SK=A
主机:127.0.0.1
来源:http://127.0.0.1
引用者:http://127.0.0.1/profile/edit
用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
X-Requested-With:XMLHttpRequest

/* 表单数据 */
a:show
id:379
key:transcript
英文:

I'm using HayaGeek's jQuery file upload plugin, and successfully post a request that can be seen on the chrome's developer tool:

/* General */
Remote Address:127.0.0.1:80
Request URL:http://127.0.0.1/profile/edit
Request Method:POST
Status Code:200 OK

/* Response Headers */
Connection:keep-alive
Content-Length:101
Content-Type:application/json
Date:Fri, 24 Apr 2015 02:04:51 GMT
Server:nginx/1.6.2
Set-Cookie:SK=A; Path=/; Expires=Fri, 24 Apr 2015 04:04:51 UTC

/* Request Headers */
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,id;q=0.6
Connection:keep-alive
Content-Length:12855
Content-Type:multipart/form-data; boundary=----WebKitFormBoundarydlN4BAQWeDyF512h
Cookie:SK=A
Host:127.0.0.1
Origin:http://127.0.0.1
Referer:http://127.0.0.1/profile/edit
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
X-Requested-With:XMLHttpRequest

/* Request Payload */
------WebKitFormBoundarydlN4BAQWeDyF512h
Content-Disposition: form-data; name="file"; filename="screen-2015-04-23-21-01-51.png"
Content-Type: image/png


------WebKitFormBoundarydlN4BAQWeDyF512h
Content-Disposition: form-data; name="a"

file_upload
------WebKitFormBoundarydlN4BAQWeDyF512h
Content-Disposition: form-data; name="key"

transcript
------WebKitFormBoundarydlN4BAQWeDyF512h
Content-Disposition: form-data; name="id"

379
------WebKitFormBoundarydlN4BAQWeDyF512h
Content-Disposition: form-data; name=""

undefined
------WebKitFormBoundarydlN4BAQWeDyF512h--

There are no problem getting the uploaded file, using this command:

file, header, err := request.FormFile(`file`) 
// `request` is `*http.Request`

But I'm getting trouble getting the rest of the POSTed parameters, I've tried these but get no luck:

 a := request.FormValue(`a`) // should be: `file_upload`
 id := request.FormValue(`id`) // should be: `transcript`
 key := request.FormValue(`key`) // should be: `379`
 // normally it's works fine, but for this plugin it doesn't work

A normal post request (without boundary, without that plugin) would give something like this, that the POSTed parameter can be retrieved normally using the command above:

/* General */
Remote Address:127.0.0.1:80
Request URL:http://127.0.0.1/profile/edit
Request Method:POST
Status Code:200 OK

/* Response Headers */
Connection:keep-alive
Content-Length:297
Content-Type:application/json
Date:Fri, 24 Apr 2015 02:17:31 GMT
Server:nginx/1.6.2
Set-Cookie:SK=A; Path=/; Expires=Fri, 24 Apr 2015 04:17:31 UTC

- Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,id;q=0.6
Connection:keep-alive
Content-Length:23
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:SK=A
Host:127.0.0.1
Origin:http://127.0.0.1
Referer:http://127.0.0.1/profile/edit
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
X-Requested-With:XMLHttpRequest

/* Form Data */
a:show
id:379
key:transcript

答案1

得分: 0

啊,我们应该首先解析MultipartForm,例如:

err := request.ParseMultipartForm(1024 * 1024 * 16) // 最大16MB
// 然后我们可以通过以下方式访问:
request.MultipartForm.Value[`a`][0]
request.MultipartForm.Value[`key`][0]
request.MultipartForm.Value[`id`][0]
request.MultipartForm.File[`file`]
英文:

Ah, we should parse MultipartForm first, for example:

err := request.ParseMultipartForm(1024 * 1024 * 16) // max 16 MB
// then we could access using:
request.MultipartForm.Value[`a`][0]
request.MultipartForm.Value[`key`][0]
request.MultipartForm.Value[`id`][0]
request.MultipartForm.File[`file`]

huangapple
  • 本文由 发表于 2015年4月24日 10:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/29837646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定