如何在 Go 中接收带有 multipart/form-data 边界的 POSTed 参数
How to receive POSTed parameter with multipart/form-data boundary in Go
我正在使用 HayaGeek's jQuery file upload 插件,并成功 post 一个可以在 chrome 的开发者工具上看到的请求:
/* 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--
使用此命令获取上传的 file
没有问题:
file, header, err := request.FormFile(`file`)
// `request` is `*http.Request`
但是我在获取其余 POST
ed 参数时遇到了问题,我已经尝试了这些但没有成功:
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
一个正常的 post 请求(没有 boundary
,没有那个插件)会给出这样的东西,POST
ed 参数可以使用上面的命令正常检索:
/* 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
啊,我们应该先解析MultipartForm
,例如:
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`]
我正在使用 HayaGeek's jQuery file upload 插件,并成功 post 一个可以在 chrome 的开发者工具上看到的请求:
/* 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--
使用此命令获取上传的 file
没有问题:
file, header, err := request.FormFile(`file`)
// `request` is `*http.Request`
但是我在获取其余 POST
ed 参数时遇到了问题,我已经尝试了这些但没有成功:
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
一个正常的 post 请求(没有 boundary
,没有那个插件)会给出这样的东西,POST
ed 参数可以使用上面的命令正常检索:
/* 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
啊,我们应该先解析MultipartForm
,例如:
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`]