django-rest-framework http在django 1.5上失败415 [英] django-rest-framework http put failing with 415 on django 1.5

查看:113
本文介绍了django-rest-framework http在django 1.5上失败415的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将django-rest-framework(最新)用于REST API,并使用内置的测试客户端在django中实现了一些测试用例。

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

在django测试之后django版本<的情况下工作正常。 1.5

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)

升级到django 1.5,除与 HTTP PUT
相关的测试外,所有测试均通过,同时调查了发现此问题的 https://docs.djangoproject.com/en/dev/releases/1.5/ #options在测试客户端中放入并删除请求

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


如果您使用的是数据在没有
content_type的PUT请求中,您必须对数据进行编码,然后再将其传递到测试
客户端并设置content_type参数。

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.

因此,更新了我的测试以反映此更改并尝试遵循,但仍然获得http 415而不是http 200

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)

知道我缺少什么吗?
PS:django-rest-framework内置的Web UI的所有功能都正常运行

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

推荐答案

绝对是正确的-那种情况下的破坏性测试肯定是由于Django更改了测试客户端的 PUT 行为。

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.

您的修复程序在我看来也。 415是不受支持的媒体类型响应,这意味着请求内容类型不是为该视图配置的任何解析器都可以处理的内容。

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.

通常,在这种情况下,这是由于忘记设置请求的内容类型而导致的,但是看起来您已经正确地将其设置为 multipart / form-data;。 boundary = ...

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=...

要再次检查的内容:


  • response.data 确切显示为错误详细信息吗?

  • 您在<$ c中配置了什么? $ c> DEFAULT_PARSER_CLASSES 设置(如果有),或者在视图属性 parser_classes 上设置了什么?

  • 确保测试中 content_type 中没有错字(即使在这里是正确的)。

  • 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).

编辑

感谢您的评论-这将清除所有内容。您仅安装了JSON解析器,但您正在尝试发送表单编码的数据。您应该:

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:


  • 添加 FormParser MultiPartParser 到您的设置/视图,以便它支持表单编码。 (另请注意,默认的 DEFAULT_PARSER_CLASSES 设置 包含了它们,因此,如果您根本不进行任何设置,它将按预期运行)

  • 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


  • 使用 json 编码而不是表单编码对请求进行编码... data = json.dumps(prepare_dict(self.account)),content_type =测试用例中的 application / json

  • Encode the request using json encoding, not form encoding... data=json.dumps(prepare_dict(self.account)), content_type='application/json' in your test case.

这篇关于django-rest-framework http在django 1.5上失败415的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆