问题描述
我想问一个关于 multipart / form-data
的问题。在HTTP标头中,我发现 Content-Type:multipart / form-data;边界= ???
。
用户是否可以自由定义 ???
>?或者它通常来自HTML?我可以定义? = abcdefg
?
是。
没有。 什么都不是。阅读下面。
是。
如果您想发送以下内容数据到Web服务器:
name = John
age = 12
使用 application / x-www-form-urlencoded
会是这样的:
name = John& age = 12
正如你所看到的,服务器知道参数被&
分开,如果&
现在,服务器如何知道参数值在何处开始和结束,当它接收到一个HTTP请求时使用
multipart / form-data
?使用边界,类似于&
。
例如:
- XXX
Content-Disposition:form-data; name =name
John
--XXX
内容处理:form-data; name =age
12
--XXX--
在这种情况下,边界值是 XXX
。您可以在 Content-Type
标头中指定它,以便服务器知道如何拆分发送的数据。
因此,您基本上需要:
- 使用一个不会出现在发送给服务器。
- 保持一致并在整个请求中使用相同的值。
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=???
.
Is the ???
free to be defined by the user? Or it is generally from the HTML? Is it possible for me to define the ??? = abcdefg
?
解决方案 Yes.
No. HTML has nothing to do with that. Read below.
Yes.
If you want to send the following data to the web server:
name = John
age = 12
using application/x-www-form-urlencoded
would be like this:
name=John&age=12
As you might see, the server knows that parameters are separated by &
, and if &
is required as a parameter value, it will be encoded.
Now, how does the server knows where a parameter value starts and ends when it receives an HTTP request using multipart/form-data
? Using the boundary, similar to &
.
For example:
--XXX
Content-Disposition: form-data; name="name"
John
--XXX
Content-Disposition: form-data; name="age"
12
--XXX--
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 sent.
So, you basically need to:
- Use a value that won't appear in the HTTP data sent to the server.
- Be consistent and use the same value all over the request.
这篇关于多部分/形式数据的边界是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!