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

Getting #39;400 Bad Request#39; when using multipart/form-data as Content-Type in XHR(在 XHR 中使用 multipart/form-data 作为 Content-Type 时收到“400 Bad Request)
本文介绍了在 XHR 中使用 multipart/form-data 作为 Content-Type 时收到“400 Bad Request"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个发送一些数据的 AJAX 请求.数据尊重 multipart/form-data 规范.

I have an AJAX request that sends out some data. The data respects the multipart/form-data specification.

我面临的问题是浏览器将 Content-Type 标头设置为 text/plain 并且它应该是 multipart/form-data.

The problem I'm facing is that the browser sets the Content-Type header to text/plain and it should be multipart/form-data.

我试过这样做:request.setRequestHeader("Content-Type", "multipart/form-data"); 但这会发出 400 Bad Request 错误.

I've tried doing this: request.setRequestHeader("Content-Type", "multipart/form-data"); but this gives out an 400 Bad Request error.

如果我这样做 request.setRequestHeader("Content-Typexxxx", "multipart/form-data"); 没有错误,则设置了Content-Typexxxx"标头,但显然是对我没有帮助.

If I do request.setRequestHeader("Content-Typexxxx", "multipart/form-data"); there is no error, the "Content-Typexxxx" header is set but it obviously is no help to me.

我猜有一个可以设置的有效 Content-Type 标头列表,multipart/form-data"不在其中,但我找不到解决我的困境的方法.

I guess there is a list of valid Content-Type headers one can set and "multipart/form-data" isn't among them, but I cannot find a sollution to my predicament.

实际发送的数据示例:

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

--l3iPy71otz
Content-Disposition: form-data; name="titluPublic"

Variation_1
--l3iPy71otz
Content-Disposition: form-data; name="nr_versiune"


--l3iPy71otz--

谢谢!

推荐答案

你没有在请求头中设置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 net 当 charset 添加到 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);

这篇关于在 XHR 中使用 multipart/form-data 作为 Content-Type 时收到“400 Bad Request"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

jasmine.createSpyObj with properties(jasmine.createSpyObj 与属性)
What#39;s a good way to reuse test code using Jasmine?(使用 Jasmine 重用测试代码的好方法是什么?)
Does Jasmine#39;s toThrow matcher require the argument to be wrapped in an anonymous function?(Jasmine 的 toThrow 匹配器是否需要将参数包装在匿名函数中?)
Jasmine - Spying on a method call within a constructor(Jasmine - 在构造函数中监视方法调用)
Getting requirejs to work with Jasmine(让 requirejs 与 Jasmine 一起工作)
How can I spy on a getter property using jasmine?(如何使用 jasmine 监视 getter 属性?)