multipart/form-data 的边界是什么?
我想问一个关于 multipart/form-data
的问题.在 HTTP 标头中,我发现 Content-Type: multipart/form-data;边界=???
.
I want to ask a question about the multipart/form-data
. In the HTTP header, I find that the Content-Type: multipart/form-data; boundary=???
.
???
可以由用户自由定义吗?还是从 HTML 生成的?我可以定义 吗???= abcdefg
?
Is the ???
free to be defined by the user? Or is it generated from the HTML? Is it possible for me to define the ??? = abcdefg
?
推荐答案
???
可以由用户自由定义吗?
Is the
???
free to be defined by the user?
是的.
还是由 HTML 提供?
or is it supplied by the HTML?
没有.HTML nothing 与此有关.阅读下文.
No. HTML has nothing to do with that. Read below.
我可以将 ???
定义为 abcdefg
吗?
Is it possible for me to define the
???
asabcdefg
?
是的.
如果您想将以下数据发送到网络服务器:
If you want to send the following data to the web server:
name = John
age = 12
使用 application/x-www-form-urlencoded
会是这样的:
name=John&age=12
如您所见,服务器知道参数是用 & 符号 &
分隔的.如果参数值需要 &
则必须对其进行编码.
As you can see, the server knows that parameters are separated by an ampersand &
. If &
is required for a parameter value then it must be encoded.
那么当服务器使用multipart/form-data
接收到一个HTTP请求时,如何知道一个参数值在哪里开始和结束呢?
So how does the server know where a parameter value starts and ends when it receives an HTTP request using multipart/form-data
?
使用边界,类似于&
.
例如:
--XXX
Content-Disposition: form-data; name="name"
John
--XXX
Content-Disposition: form-data; name="age"
12
--XXX--
在这种情况下,边界值为XXX
.您在 Content-Type
标头中指定它,以便服务器知道如何拆分它接收到的数据.
In that case, the boundary value is XXX
. You specify it in the Content-Type
header so that the server knows how to split the data it receives.
所以你需要:
使用不会出现在发送到服务器的 HTTP 数据中的值.
Use a value that won't appear in the HTTP data sent to the server.
保持一致并在请求消息中的所有位置使用相同的值.
Be consistent and use the same value everywhere in the request message.
相关文章