django-rest-framework http put failing with 415 on django 1.5
You're absolutely on the right track - the breaking test in that case is certainly due to Django's change in PUT
behavior for the test client.
Your fix looks right to me, too. 415 is the "Unsupported Media Type" response, which means that the request content type wasn't something that could be handled by any of the parsers configured for the view.
Normally in case like this, that'd be due to forgetting to set the content type of the request, but it looks like you've got that correctly set to multipart/form-data; boundary=...
Things to double check:
- Exactly what does
response.data
display as the error details? - What do you have configured in you
DEFAULT_PARSER_CLASSES
setting, if you have one, or what do you have set on the view attributeparser_classes
if it has one? - Make sure there's not a typo in
content_type
in the test (even though it's correct here).
Edit:
Thanks for your comments - that clears everything up. You've only got the JSON parser installed, but you're trying to send Form encoded data. You should either:
- Add
FormParser
andMultiPartParser
to your settings/view, so that it supports form encodings. (Note also that the defaultDEFAULT_PARSER_CLASSES
setting does include them, so if you don't set anything at all it'll work as expected)
Or
- Encode the request using
json
encoding, not form encoding...data=json.dumps(prepare_dict(self.account)), content_type='application/json'
in your test case.
Got a 415 error because I used an instance of django.test import Client
instead of rest_framework.test import APIClient
. APIClient
will encode the data automatically in it's right way.
Pure json request:
client = APIClient()
client.post(url, format='json', data=json, headers=headers)
client.put(url, format='json', data=json, headers=headers)
Create/Update including file(s):
client = APIClient()
client.post(url, format='multipart', data=data, headers=headers)
client.put(url, format='multipart', data=data, headers=headers)
I spent a lot of time with this 415 error, so I hope this helps anyone else.