JSON在Django Rest API中使用POST解析错误 [英] Json parse error using POST in django rest api
问题描述
我正在尝试通过Django REST框架实现一个简单的GET/POST api
I am trying to implement a simple GET/POST api via Django REST framework
views.py
class cuser(APIView):
def post(self, request):
stream = BytesIO(request.DATA)
json = JSONParser().parse(stream)
return Response()
urls.py
from django.conf.urls import patterns, url
from app import views
urlpatterns = patterns('',
url(r'^challenges/',views.getall.as_view() ),
url(r'^cuser/' , views.cuser.as_view() ),
)
我正在尝试POST
一些json到/api/cuser/
(api是项目的urls.py
中的名称空间),
JSON
I am trying to POST
some json to /api/cuser/
(api is namespace in my project's urls.py
) ,
the JSON
{
"username" : "abhishek",
"email" : "john@doe.com",
"password" : "secretpass"
}
我在Browseable API页面和httpie
(类似于curl的python制作的工具)上都尝试过
I tried from both Browseable API page and httpie
( A python made tool similar to curl)
httpie command
http --json POST http://localhost:58601/api/cuser/ username=abhishek email=john@doe.com password=secretpass
但是我收到JSON解析错误:
but I am getting JSON parse error :
JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Whole Debug message using --verbose --debug
POST /api/cuser/ HTTP/1.1
Content-Length: 75
Accept-Encoding: gzip, deflate
Host: localhost:55392
Accept: application/json
User-Agent: HTTPie/0.8.0
Connection: keep-alive
Content-Type: application/json; charset=utf-8
{"username": "abhishek", "email": "john@doe.com", "password": "aaezaakmi1"}
HTTP/1.0 400 BAD REQUEST
Date: Sat, 24 Jan 2015 09:40:03 GMT
Server: WSGIServer/0.1 Python/2.7.9
Vary: Accept, Cookie
Content-Type: application/json
Allow: POST, OPTIONS
{"detail":"JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"}
推荐答案
您遇到的问题是您的请求已经被解析,并且您试图再次解析它.
The problem that you are running into is that your request is already being parsed, and you are trying to parse it a second time.
来自解析器如何确定"
视图的有效解析器集始终定义为类列表.当访问
request.data
时,REST框架将检查传入请求的Content-Type标头,并确定使用哪个解析器来解析请求内容.
The set of valid parsers for a view is always defined as a list of classes. When
request.data
is accessed, REST framework will examine the Content-Type header on the incoming request, and determine which parser to use to parse the request content.
在您的代码中,您正在访问request.DATA
,它等同于request.data
的2.4.x.因此,您的请求一被调用就被解析,而request.DATA
实际上正在返回您希望解析的字典.
In your code you are accessing request.DATA
, which is the 2.4.x equaivalent of request.data
. So your request is being parsed as soon as you call that, and request.DATA
is actually returning the dictionary that you were expecting to parse.
json = request.DATA
实际上就是解析传入的JSON数据所需的全部.您实际上是在将Python字典传递给json.loads
,该字典似乎无法解析它,这就是为什么出现错误的原因.
is really all you need to parse the incoming JSON data. You were really passing a Python dictionary into json.loads
, which does not appear to be able to parse it, and that is why you were getting your error.
这篇关于JSON在Django Rest API中使用POST解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!