gpt4 book ai didi

在 django Rest api 中使用 POST 进行 Json 解析错误

转载 作者:行者123 更新时间:2023-12-02 06:51:07 25 4
gpt4 key购买 nike

我正在尝试通过 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"
}

我尝试从可浏览的API页面和httpie(一个类似于curl的python工具)

httpie 命令

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)

使用 --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)"}

最佳答案

我通过 Google 到达此帖子

"detail": "JSON parse error - Expecting property name enclosed in double-quotes": Turns out you CANNOT have a trailing comma in JSON.

因此,如果您收到此错误,您可能需要更改如下帖子:

{
"username" : "abhishek",
"email" : "john@doe.com",
"password" : "secretpass",
}

对此:

{
"username" : "abhishek",
"email" : "john@doe.com",
"password" : "secretpass"
}

请注意 JSON 对象中最后一个属性后面删除的逗号

关于在 django Rest api 中使用 POST 进行 Json 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28114514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com