且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 XHR 中使用 multipart/form-data 作为 Content-Type 时收到“400 Bad Request"

更新时间:2021-09-01 08:32:44

你没有在请求头中设置boundary,如:

You didn't set the boundary in your request header, as in:

request.setRequestHeader("Content-Type", "multipart/form-data; boundary=l3iPy71otz");

有关详细信息,请参阅 RFC 2045:

For more information, see RFC 2045:

5 内容类型标题字段
[…]
参数是媒体的修饰符子类型,因此不要从根本上影响性质内容.有意义的集合参数取决于媒体类型和亚型.大多数参数是与单个特定的相关联亚型.然而,一个给定的***媒体类型可以定义适用的参数该类型的任何子类型.参数可能需要他们的定义内容类型或子类型,或者它们可能是选修的.MIME 实现必须忽略其名称的任何参数不认识.

5 Content-Type Header Field
[…]
Parameters are modifiers of the media subtype, and as such do not fundamentally affect the nature of the content. The set of meaningful parameters depends on the media type and subtype. Most parameters are associated with a single specific subtype. However, a given top-level media type may define parameters which are applicable to any subtype of that type. Parameters may be required by their defining content type or subtype or they may be optional. MIME implementations must ignore any parameters whose names they do not recognize.

例如,字符集"参数适用于任何子类型文本",而边界"任何子类型都需要参数多部分"媒体类型.

For example, the "charset" parameter is applicable to any subtype of "text", while the "boundary" parameter is required for any subtype of the "multipart" media type.

更新:我发现的另一个问题 on the netcharset 添加到 Content-type 时出现code> 在请求标头中,但不在正文中的消息边界中(这也适用于您的测试用例).这似乎不是一个可行的解决方案,但也许会有所帮助.

Update: Another problem I found on the net appears when a charset is added to the Content-type in the request header, but not in the message boundaries in the body (this is also true for your test case). It doesn't seem a likely solution, but perhaps it helps.

在您的情况下,将 charset 显式添加到请求标头和消息边界中:

In your case, explicitly add a charset to both the request header and in the message boundaries:

data.params += "--" + data.uniqid + "; charset=UTF-8" + data.crlf;
…
request.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + data.uniqid + "; charset=UTF-8");

更新 2: 在我自己在本地尝试后,我注意到前导边界没有被识别为这样,而是被解释为最后一个参数内容(在我更宽容的服务器上).也许这导致 Apache 抛出 400 Bad Request 错误.

Update 2: After trying this myself locally, I noticed the leading boundary wasn't recognized as such, but interpreted as the last parameter contents (on my more forgiving server). Perhaps that was causing Apache to throw a 400 Bad Request error.

经过反复试验,我注意到这是因为服务器期望 charset 位于 every 边界,甚至是最后一个边界.为了防止混淆,我决定在请求头 before 边界参数中显式设置 charset,以便边界将是 Content- 中的最后一个参数类型 请求头.在此之后,一切似乎都运行良好.

After some trial and error, I noticed that that was caused because the server expected the charset to be in every boundary, even the last one. To prevent confusion, I decided to explicitly set the charset in the request header before the boundary parameter, so that the boundary would be the last parameter in the Content-type request header. After this, everything seemed to work fine.

data.params = "Content-Type: multipart/form-data; boundary=" + data.uniqid;
…
data.params += "--" + data.uniqid + data.crlf;
…
data.params += "--" + data.uniqid + "--";
…
request.setRequestHeader("Content-Type", "multipart/form-data; charset=UTF-8; boundary=" + data.uniqid);