Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

django-rest-framework http put failing with 415 on django 1.5

I'm using django-rest-framework (latest) for REST API, and implemented few test cases in django using built in test client.

following django test case was working fine with django version < 1.5

self.client.put('/core/accounts/%s/'% self.account.id,
        data = prepare_dict(self.account),
        HTTP_AUTHORIZATION=self.token)

upgraded to django 1.5, all tests are passing except tests related to HTTP PUT while looking into the issue found this @ https://docs.djangoproject.com/en/dev/releases/1.5/#options-put-and-delete-requests-in-the-test-client

If you were using the data parameter in a PUT request without a content_type, you must encode your data before passing it to the test client and set the content_type argument.

So, updated my test to reflect this change and tried following, but still getting http 415 instead of http 200

from django.test.client import MULTIPART_CONTENT, BOUNDARY, encode_multipart
self.client.put('/core/accounts/%s/'% self.account.id,
            data = encode_multipart(BOUNDARY, prepare_dict(self.account)),
                content_type=MULTIPART_CONTENT,
        HTTP_AUTHORIZATION=self.token)

Any idea what I'm missing? P.S: All functionality is working fine from django-rest-framework built-in web UI

question from:https://stackoverflow.com/questions/15153048/django-rest-framework-http-put-failing-with-415-on-django-1-5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 attribute parser_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 and MultiPartParser to your settings/view, so that it supports form encodings. (Note also that the default DEFAULT_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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...