【发布时间】:2015-03-22 18:20:29
【问题描述】:
我正在尝试通过 Django REST 框架实现一个简单的 GET/POST api
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
{
"username" : "abhishek",
"email" : "john@doe.com",
"password" : "secretpass"
}
我从 Browseable API 页面和httpie(类似于 curl 的 python 制作工具)都试过了
httpie command
http --json POST http://localhost:58601/api/cuser/ username=abhishek email=john@doe.com password=secretpass
但我收到 JSON 解析错误:
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)"}
【问题讨论】:
-
请准确地显示您是如何发布该 JSON 的。从哪里来?
-
@DanielRoseman 尝试了
httpie和可浏览的 api 页面 -
尝试在 HTTPie 命令中添加
--verbose --debug。这将向您显示正在发送的请求以及一些额外的调试输出。 -
@JakubRoztočil 添加了调试输出,虽然我无法从中获得太多收益
-
我认为服务器端存在一些错误。请求/JSON 看起来是正确的。您也可以尝试向
httpbin.org发送相同的请求,看看它是如何被解释的:$ http --json httpbin.org/post username=abhishek email=john@doe.com password=secretpass
标签: json django rest django-rest-framework