Html 多部分/表单数据的边界是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3508338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:11:07  来源:igfitidea点击:

What is the boundary in multipart/form-data?

htmlhttpforms

提问by Questions

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=???.

我想问一个关于multipart/form-data. 在 HTTP 标头中,我发现Content-Type: multipart/form-data; boundary=???.

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?

???免费的由用户来定义?或者它是从 HTML 生成的?我可以定义??? = abcdefg吗?

回答by Oscar Mederos

Is the ???free to be defined by the user?

???免费的由用户来定义?

Yes.

是的。

or is it supplied by the HTML?

还是由 HTML 提供?

No. HTMLhas nothingto do with that. Read below.

HTML没有做到这一点。参见下文。

Is it possible for me to define the ???as abcdefg?

我可以将其定义???abcdefg吗?

Yes.

是的。

If you want to send the following data to the web server:

如果要将以下数据发送到 Web 服务器:

name = John
age = 12

using application/x-www-form-urlencodedwould be like this:

使用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.

如您所见,服务器知道参数之间用与号分隔&。如果&需要参数值,则必须对其进行编码。

So how does the server know where a parameter value starts and ends when it receives an HTTP request using multipart/form-data?

那么当服务器接收到一个使用 的 HTTP 请求时,它是如何知道参数值的开始和结束位置的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-Typeheader so that the server knows how to splitthe data it receives.

在这种情况下,边界值为XXX。您在Content-Type标头中指定它,以便服务器知道如何拆分它收到的数据。

So you need to:

所以你需要:

  • 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.

  • 使用不会出现在发送到服务器的 HTTP 数据中的值。

  • 保持一致并在请求消息中的任何地方使用相同的值。

回答by antichris

The exact answer to the question is: yes, you can use an arbitrary value for the boundaryparameter, given it does not exceed 70 bytes in length and consists only of 7-bit US-ASCII(printable) characters.

问题的确切答案是:是的,您可以对boundary参数使用任意值,前提是它的长度不超过 70 个字节并且仅包含7 位US-ASCII(可打印)字符。

If you are using one of multipart/*content types, you are actually requiredto specify the boundaryparameter in the Content-Typeheader, otherwise the server (in the case of an HTTP request) will not be able to parse the payload.

如果您使用其中一种multipart/*内容类型,则实际上需要boundaryContent-Type标头中指定参数,否则服务器(在 HTTP 请求的情况下)将无法解析有效负载。

You probably also want to set the charsetparameter to UTF-8in your Content-Typeheader, unless you can be absolutelysure that only US-ASCIIcharset will be used in the payload data.

您可能还希望将charset参数设置为UTF-8在您的Content-Type标头中,除非您可以绝对确定US-ASCII在有效负载数据中仅使用字符集。

A few relevant excerpts from the RFC2046:

RFC2046 的一些相关摘录:

  • 4.1.2. Charset Parameter:

    Unlike some other parameter values, the values of the charset parameter are NOT case sensitive. The default character set, which must be assumed in the absence of a charset parameter, is US-ASCII.

  • 5.1. Multipart Media Type

    As stated in the definition of the Content-Transfer-Encoding field [RFC 2045], no encoding other than "7bit", "8bit", or "binary" is permitted for entities of type "multipart". The "multipart" boundary delimiters and header fields are always represented as 7bit US-ASCII in any case (though the header fields may encode non-US-ASCII header text as per RFC 2047) and data within the body parts can be encoded on a part-by-part basis, with Content-Transfer-Encoding fields for each appropriate body part.

    The Content-Type field for multipart entities requires one parameter, "boundary". The boundary delimiter line is then defined as a line consisting entirely of two hyphen characters ("-", decimal value 45) followed by the boundary parameter value from the Content-Type header field, optional linear whitespace, and a terminating CRLF.

    Boundary delimiters must not appear within the encapsulated material, and must be no longer than 70 characters, not counting the two leading hyphens.

    The boundary delimiter line following the last body part is a distinguished delimiter that indicates that no further body parts will follow. Such a delimiter line is identical to the previous delimiter lines, with the addition of two more hyphens after the boundary parameter value.

  • 4.1.2. 字符集参数:

    与其他一些参数值不同,charset 参数的值不区分大小写。在没有 charset 参数的情况下必须假定默认字符集是 US-ASCII。

  • 5.1. 多部分媒体类型

    如内容传输编码字段 [RFC 2045] 的定义中所述,对于“multipart”类型的实体,不允许使用除“7bit”、“8bit”或“binary”以外的任何编码。在任何情况下,“多部分”边界分隔符和标题字段始终表示为 7 位 US-ASCII(尽管标题字段可以根据 RFC 2047 对非 US-ASCII 标题文本进行编码),并且正文部分中的数据可以在逐个部分的基础,每个适当的正文部分都有内容传输编码字段。

    多部分实体的 Content-Type 字段需要一个参数“边界”。然后,边界定界符行被定义为完全由两个连字符(“-”,十进制值 45)组成的行,后跟来自 Content-Type 头字段的边界参数值、可选的线性空白和终止 CRLF。

    边界定界符不得出现在封装材料中,并且不得超过 70 个字符,不包括两个前导连字符。

    最后一个正文部分后面的边界定界符线是一个显着的定界符,表示后面不会有其他正文部分。这样的定界符行与前面的定界符行相同,只是在边界参数值之后添加了两个连字符。

Here is an example using an arbitrary boundary:

这是使用任意边界的示例:

Content-Type: multipart/form-data; charset=utf-8; boundary="another cool boundary"

--another cool boundary
Content-Disposition: form-data; name="foo"

bar
--another cool boundary
Content-Disposition: form-data; name="baz"

quux
--another cool boundary--

回答by Yergalem

multipart/form-data contains boundaryto separate name/value pairs. The boundary acts like a marker of each chunk of name/value pairs passed when a form gets submitted. The boundary is automatically added to a content-type of a request header.

multipart/form-data 包含分隔名称/值对的边界。边界就像提交表单时传递的每个名称/值对块的标记。边界会自动添加到请求标头的内容类型中。

The form with enctype="multipart/form-data"attribute will have a request header Content-Type : multipart/form-data; boundary --- WebKit193844043-h (browser generated vaue).

带有enctype="multipart/form-data"属性的表单会有一个请求头 Content-Type : multipart/form-data; 边界 --- WebKit193844043-h(浏览器生成的 vaue)。

The payload passed looks something like this:

传递的有效载荷如下所示:

Content-Type: multipart/form-data; boundary=---WebKitFormBoundary7MA4YWxkTrZu0gW

    -----WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name=”file”; filename=”captcha”
    Content-Type:

    -----WebKitFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name=”action”

    submit
    -----WebKitFormBoundary7MA4YWxkTrZu0gW--

On the webservice side, it's consumed in @Consumes("multipart/form-data") form.

在 webservice 端,它以 @Consumes("multipart/form-data") 形式消费。

Beware, when testing your webservice using chrome postman, you need to check the form data option(radio button) and File menu from the dropdown box to send attachment. Explicit provision of content-type as multipart/form-data throws an error. Because boundary is missing as it overrides the curl request of post man to server with content-type by appending the boundary which works fine.

请注意,在使用 chrome postman 测试您的网络服务时,您需要从下拉框中检查表单数据选项(单选按钮)和文件菜单以发送附件。将内容类型显式提供为 multipart/form-data 会引发错误。因为缺少边界,因为它通过附加工作正常的边界来覆盖邮递员对具有内容类型的服务器的 curl 请求。

See RFC1341 sec7.2 The Multipart Content-Type

请参阅RFC1341 sec7.2 多部分内容类型