小编use*_*001的帖子

Json在django rest api中使用POST解析错误

我试图通过Django REST框架实现一个简单的GET/POST api

views.py

class cuser(APIView):
def post(self, request):
   stream  = BytesIO(request.DATA)
    json = JSONParser().parse(stream)
    return Response()
Run Code Online (Sandbox Code Playgroud)

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() ),
      )
Run Code Online (Sandbox Code Playgroud)

我正在尝试POST一些json /api/cuser/(api是我项目中的命名空间urls.py),JSON

{
"username" : "abhishek",
"email" : "john@doe.com",
"password" : "secretpass"
}
Run Code Online (Sandbox Code Playgroud)

我尝试了从Browseable API页面和httpie(类似于curl的python制作工具)

httpie command

http --json POST http://localhost:58601/api/cuser/ username=abhishek email=john@doe.com password=secretpass
Run Code Online (Sandbox Code Playgroud)

但我得到JSON解析错误:

JSON parse error - Expecting property name enclosed in double …
Run Code Online (Sandbox Code Playgroud)

django rest json django-rest-framework

7
推荐指数
2
解决办法
2万
查看次数

标签 统计

django ×1

django-rest-framework ×1

json ×1

rest ×1