Django无法访问raw_post_data [英] Django can't access raw_post_data

查看:48
本文介绍了Django无法访问raw_post_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django上遇到了一件奇怪的事情,这是我的views.py:

I am experiencing a strange thing with Django, here is my views.py:

def api(request):
    return HttpResponse("%s %s" % (request.method,request.raw_post_data))

现在,我使用POSTMAN(适用于Google chrome的小型应用程序)进行HTTP POST.

Now I make an HTTP POST with POSTMAN (small app for google chrome).

我将POSTMAN设置为在原始字段中使用测试"发出POST请求.

I set POSTMAN to make a POST request with 'test' in the raw field.

Django给我3种不同的东西(随机):

Django returns me 3 different thing (random):

有时候Django有时有时什么也没有返回"GET":

Sometime Django returns 'GET' sometime nothing at all and sometime:

AttributeError at /
'WSGIRequest' object has no attribute 'raw_post_data'
Request Method: GET
Request URL:    https://api.mywebsiteurl.com/
Django Version: 1.6.2
Exception Type: AttributeError
Exception Value:    
'WSGIRequest' object has no attribute 'raw_post_data'
Exception Location: /home/spice_dj/spice/views.py in api, line 17
Python Executable:  /usr/bin/python
Python Version: 2.7.3
Python Path:    
['/usr/local/lib/python2.7/dist-packages/South-0.8.4-py2.7.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-linux2',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/home/spice_dj']
Server time:    Wed, 12 Mar 2014 22:51:11 -0400

  1. 为什么当我清楚地发出POST请求时Django返回我"GET"?

  1. Why Django returns me 'GET' when I clearly make a POST request?

为什么会返回该错误?

为什么它不返回我在原始字段中设置的测试"?

Why it does not return me the 'test' that I set in the raw field?

推荐答案

根据

属性 HttpRequest.raw_post_data 重命名为1.4中的 HttpRequest.body .向后兼容性将被删除– HttpRequest.raw_post_data 将不再起作用.

The attribute HttpRequest.raw_post_data was renamed to HttpRequest.body in 1.4. The backward compatibility will be removed – HttpRequest.raw_post_data will no longer work.

相关票据中描述了动机:

The motivation is described in the relevant ticket:

request.raw_post_dat a是一个不好的名字.它与 POST 中的无关特别是,它只是HTTP请求的主体.这令人困惑用户,并使其看起来像Django不了解HTTP作品.我们应该将名称更改为 request.body 并开始弃用过程.

request.raw_post_data is a bad name. It has nothing to do with POST in particular, it's just the body of the HTTP request. This confuses users, and makes it look like Django doesn't understand how HTTP works. We should change the name to request.body and start a deprecation process.

使用 request.body :

def api(request):
    return HttpResponse("%s %s" % (request.method, request.body))

希望有帮助.

这篇关于Django无法访问raw_post_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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