Reputation: 1199
[SOLVED] Please see my answer.
Any POST
request sent with the
Content-Type: multipart/form-data; boundary=xYzZY
results in the request.POST
QueryDict{}
to be empty. Changing the Content-Type
to multipart/form-data
also results in the same error.
Removing the Content-Type altogether results in the values getting passed correctly, and I can access them in request.POST
.
I have tried disabling the Csrf middleware, using @csrf_exempt, and also tried the same on multiple servers. Didn't change the empty POST condition.
While reading up on Django framework and POST content-type, I read that it (no longer) assumes a default content-type and therefore must be supplied with the correct one (I do not have a link to the article in question.) I think something similar is happening here, with django not being able to parse the parameters with the given content-type (but leaving it blank lets the parser interpret it with the default value).
What I am having trouble with is, that the Content-Type value supplied is perfectly valid (multipart/form-data with boundary). So why does django refuse to load it in the POST dictionary?
** I am not in control of the Content-Type sent in the POST data.
** UPDATE: reading from request.body
shows that all the POST
parameters are being received. They're just not present in request.POST
** UPDATE: I'm using Runscope to test POST
requests.
Upvotes: 0
Views: 2281
Reputation: 1199
As mentioned in the UPDATE, I was using Runscope to test the POST
data. I realised that the error was with the way Runscope handled multipart/form-data
. I raised the issue with support and got notified that Runscope does not support multipart as of now. I've copied the relevant information here:
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.
Using another service solved this for me.
Upvotes: 1
Reputation: 21789
You are (in a manner) in control of Content-Type
. What you're looking for is enctype
. You can use it as following:
<form method="POST" action="." enctype="multipart/form-data">
enctype
is only required when you are uploading a file, otherwise, it's not.
Upvotes: 0