多部分/表单数据示例 [英] Example of multipart/form-data

查看:41
本文介绍了多部分/表单数据示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以与我分享包含以下内容的多部分/表单数据示例:

I am wondering if anyone can share with me an example of multipart/form-data that contains:

  1. 一些表单参数
  2. 多个文件

推荐答案

EDIT:我正在维护一个类似但更深入的答案:https://stackoverflow.com/a/28380690/895245

EDIT: I am maintaining a similar, but more in-depth answer at: https://stackoverflow.com/a/28380690/895245

要确切了解发生了什么,请使用 nc -l 或 ECHO 服务器和用户代理(如浏览器或 cURL).

To see exactly what is happening, use nc -l or an ECHO server and an user agent like a browser or cURL.

将表单保存到 .html 文件:

Save the form to an .html file:

<form action="http://localhost:8000" method="post" enctype="multipart/form-data">
  <p><input type="text" name="text" value="text default">
  <p><input type="file" name="file1">
  <p><input type="file" name="file2">
  <p><button type="submit">Submit</button>
</form>

创建要上传的文件:

echo 'Content of a.txt.' > a.txt
echo '<!DOCTYPE html><title>Content of a.html.</title>' > a.html

运行:

nc -l localhost 8000

在浏览器上打开 HTML,选择文件并点击提交并检查终端.

Open the HTML on your browser, select the files and click on submit and check the terminal.

nc 打印收到的请求.火狐发送:

nc prints the request received. Firefox sent:

POST / HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __atuvc=34%7C7; permanent=0; _gitlab_session=226ad8a0be43681acf38c2fab9497240; __profilin=p%3Dt; request_method=GET
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266
Content-Length: 554

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="text"

text default
-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain

Content of a.txt.

-----------------------------9051914041544843365972754266
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html

<!DOCTYPE html><title>Content of a.html.</title>

-----------------------------9051914041544843365972754266--

或者,cURL 应该发送与浏览器表单相同的 POST 请求:

Aternativelly, cURL should send the same POST request as your a browser form:

nc -l localhost 8000
curl -F "text=default" -F "file1=@a.html" -F "file1=@a.txt" localhost:8000

您可以使用以下方法进行多项测试:

You can do multiple tests with:

while true; do printf '' | nc -l localhost 8000; done

这篇关于多部分/表单数据示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆