request.POST 内容类型为空:multipart/form-data;边界=xYzZY

request.POST empty for Content-Type: multipart/form-data; boundary=xYzZY

[已解决]请看我的回答。

任何 POST 请求随 Content-Type: multipart/form-data; boundary=xYzZY 导致 request.POST QueryDict{} 为空。将 Content-Type 更改为 multipart/form-data 也会导致相同的错误。

完全删除 Content-Type 会导致值正确传递,我可以在 request.POST.

中访问它们

我试过禁用 Csrf 中间件,使用@csrf_exempt,也在多台服务器上尝试过同样的方法。没有改变空 POST 条件。

在阅读 Django 框架和 POST 内容类型时,我读到它(不再)采用默认内容类型,因此必须提供正确的内容类型(我没有link 到有问题的文章。)我认为这里发生了类似的事情,django 无法解析具有给定内容类型的参数(但将其留空让解析器使用默认值解释它) .

我遇到的问题是,提供的 Content-Type 值完全有效(multipart/form-data 有边界)。那么,为什么 django 拒绝将其加载到 POST 字典中呢?

** 我无法控制 POST 数据中发送的内容类型。

** 更新:从 request.body 读取显示正在接收所有 POST 参数。它们只是不存在于 request.POST

** 更新:我正在使用 Runscope 测试 POST 请求。

您(在某种程度上)控制着 Content-Type。您要查找的是 enctype。您可以按如下方式使用它:

<form method="POST" action="." enctype="multipart/form-data">

enctype只在上传文件时需要,否则不需要。

UPDATE 中所述,我使用 Runscope 来测试 POST 数据。我意识到错误与 Runscope 处理 multipart/form-data 的方式有关。我向支持人员提出了这个问题,并得到通知说 Runscope 目前不支持多部分。我把相关资料复制到这里:

We hope to support multipart form uploads capabilities for the future, but don't have a timeline on when this will be available. Some customers have made this work in their Radar tests (https://www.runscope.com/docs/radar) by pasting in the raw multipart-formatted body or unicode string input body into the request and making sure to include the applicable 'Content-type' header value with the correct boundaries. Some examples for building a multipart/form-data POST request can be found here: http://chxo.com/be2/20050724_93bf.html

For Runscope URLs, multipart data is passed through unmodified. However, the request editor and retries from the Traffic Inspector (https://www.runscope.com/docs/inspector) do not currently support multipart data which is why your request retry did not work. Additionally, request and response bodies larger than 1 MB are not saved for viewing after the request has been sent.

使用其他服务为我解决了这个问题。