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

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

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

问题

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

  1. /* 通用 */
  2. 远程地址:127.0.0.1:80
  3. 请求URLhttp://127.0.0.1/profile/edit
  4. 请求方法:POST
  5. 状态码:200 OK
  6. /* 响应头 */
  7. 连接:保持连接
  8. 内容长度:101
  9. 内容类型:application/json
  10. 日期:Fri, 24 Apr 2015 02:04:51 GMT
  11. 服务器:nginx/1.6.2
  12. 设置CookieSK=A; 路径=/; 过期时间:Fri, 24 Apr 2015 04:04:51 UTC
  13. /* 请求头 */
  14. 接受:*/*
  15. 接受编码:gzip, deflate
  16. 接受语言:en-US,en;q=0.8,id;q=0.6
  17. 连接:保持连接
  18. 内容长度:12855
  19. 内容类型:multipart/form-data; boundary=----WebKitFormBoundarydlN4BAQWeDyF512h
  20. Cookie:SK=A
  21. 主机:127.0.0.1
  22. 来源:http://127.0.0.1
  23. 引用者:http://127.0.0.1/profile/edit
  24. 用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
  25. X-Requested-With:XMLHttpRequest
  26. /* 请求有效载荷 */
  27. ------WebKitFormBoundarydlN4BAQWeDyF512h
  28. 内容分配:form-data; name="file"; filename="screen-2015-04-23-21-01-51.png"
  29. 内容类型:image/png
  30. ------WebKitFormBoundarydlN4BAQWeDyF512h
  31. 内容分配:form-data; name="a"
  32. file_upload
  33. ------WebKitFormBoundarydlN4BAQWeDyF512h
  34. 内容分配:form-data; name="key"
  35. transcript
  36. ------WebKitFormBoundarydlN4BAQWeDyF512h
  37. 内容分配:form-data; name="id"
  38. 379
  39. ------WebKitFormBoundarydlN4BAQWeDyF512h
  40. 内容分配:form-data; name=""
  41. undefined
  42. ------WebKitFormBoundarydlN4BAQWeDyF512h--

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

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

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

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

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

  1. /* 通用 */
  2. 远程地址:127.0.0.1:80
  3. 请求URLhttp://127.0.0.1/profile/edit
  4. 请求方法:POST
  5. 状态码:200 OK
  6. /* 响应头 */
  7. 连接:保持连接
  8. 内容长度:297
  9. 内容类型:application/json
  10. 日期:Fri, 24 Apr 2015 02:17:31 GMT
  11. 服务器:nginx/1.6.2
  12. 设置CookieSK=A; 路径=/; 过期时间:Fri, 24 Apr 2015 04:17:31 UTC
  13. - 请求头
  14. 接受:*/*
  15. 接受编码:gzip, deflate
  16. 接受语言:en-US,en;q=0.8,id;q=0.6
  17. 连接:保持连接
  18. 内容长度:23
  19. 内容类型:application/x-www-form-urlencoded; charset=UTF-8
  20. Cookie:SK=A
  21. 主机:127.0.0.1
  22. 来源:http://127.0.0.1
  23. 引用者:http://127.0.0.1/profile/edit
  24. 用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
  25. X-Requested-With:XMLHttpRequest
  26. /* 表单数据 */
  27. a:show
  28. id:379
  29. 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:

  1. /* General */
  2. Remote Address:127.0.0.1:80
  3. Request URL:http://127.0.0.1/profile/edit
  4. Request Method:POST
  5. Status Code:200 OK
  6. /* Response Headers */
  7. Connection:keep-alive
  8. Content-Length:101
  9. Content-Type:application/json
  10. Date:Fri, 24 Apr 2015 02:04:51 GMT
  11. Server:nginx/1.6.2
  12. Set-Cookie:SK=A; Path=/; Expires=Fri, 24 Apr 2015 04:04:51 UTC
  13. /* Request Headers */
  14. Accept:*/*
  15. Accept-Encoding:gzip, deflate
  16. Accept-Language:en-US,en;q=0.8,id;q=0.6
  17. Connection:keep-alive
  18. Content-Length:12855
  19. Content-Type:multipart/form-data; boundary=----WebKitFormBoundarydlN4BAQWeDyF512h
  20. Cookie:SK=A
  21. Host:127.0.0.1
  22. Origin:http://127.0.0.1
  23. Referer:http://127.0.0.1/profile/edit
  24. User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
  25. X-Requested-With:XMLHttpRequest
  26. /* Request Payload */
  27. ------WebKitFormBoundarydlN4BAQWeDyF512h
  28. Content-Disposition: form-data; name="file"; filename="screen-2015-04-23-21-01-51.png"
  29. Content-Type: image/png
  30. ------WebKitFormBoundarydlN4BAQWeDyF512h
  31. Content-Disposition: form-data; name="a"
  32. file_upload
  33. ------WebKitFormBoundarydlN4BAQWeDyF512h
  34. Content-Disposition: form-data; name="key"
  35. transcript
  36. ------WebKitFormBoundarydlN4BAQWeDyF512h
  37. Content-Disposition: form-data; name="id"
  38. 379
  39. ------WebKitFormBoundarydlN4BAQWeDyF512h
  40. Content-Disposition: form-data; name=""
  41. undefined
  42. ------WebKitFormBoundarydlN4BAQWeDyF512h--

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

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

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

  1. a := request.FormValue(`a`) // should be: `file_upload`
  2. id := request.FormValue(`id`) // should be: `transcript`
  3. key := request.FormValue(`key`) // should be: `379`
  4. // 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:

  1. /* General */
  2. Remote Address:127.0.0.1:80
  3. Request URL:http://127.0.0.1/profile/edit
  4. Request Method:POST
  5. Status Code:200 OK
  6. /* Response Headers */
  7. Connection:keep-alive
  8. Content-Length:297
  9. Content-Type:application/json
  10. Date:Fri, 24 Apr 2015 02:17:31 GMT
  11. Server:nginx/1.6.2
  12. Set-Cookie:SK=A; Path=/; Expires=Fri, 24 Apr 2015 04:17:31 UTC
  13. - Request Headers
  14. Accept:*/*
  15. Accept-Encoding:gzip, deflate
  16. Accept-Language:en-US,en;q=0.8,id;q=0.6
  17. Connection:keep-alive
  18. Content-Length:23
  19. Content-Type:application/x-www-form-urlencoded; charset=UTF-8
  20. Cookie:SK=A
  21. Host:127.0.0.1
  22. Origin:http://127.0.0.1
  23. Referer:http://127.0.0.1/profile/edit
  24. User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
  25. X-Requested-With:XMLHttpRequest
  26. /* Form Data */
  27. a:show
  28. id:379
  29. key:transcript

答案1

得分: 0

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

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

Ah, we should parse MultipartForm first, for example:

  1. err := request.ParseMultipartForm(1024 * 1024 * 16) // max 16 MB
  2. // then we could access using:
  3. request.MultipartForm.Value[`a`][0]
  4. request.MultipartForm.Value[`key`][0]
  5. request.MultipartForm.Value[`id`][0]
  6. 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:

确定