没有JSON对象可以被解码 - tastypie - curl

nkn*_*knj 6 python django json curl tastypie

我一个字一个地跟着tastypie教程,直到我到达帖子部分:http://django-tastypie.readthedocs.org/en/latest/interacting.html#creating-a-new-resource-post

当我运行此命令时,我不断收到以下错误: No JSON object could be decoded

我查了一下,我确信我正在逐字逐句地遵循文件.

谢谢你的帮助

nkn*_*knj 16

原来是cURL的windows事物.

  1. 应使用双引号("")而不是单引号引用JSON数据.
  2. 必须使用反斜杠(\)转义json数据包中的所有双引号

例如:所以,这个:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body": "This will prbbly be my lst post.", "pub_date": "2011-05-22T00:46:38", "slug": "another-post", "title": "Another Post", "user": "/api/v1/user/1/"}' http://localhost:8000/api/v1/entry/

应该:

curl --dump-header - -H "Content-Type: application/json" -X POST --data "{\"body\": \"This will prbbly be my lst post.\", \"pub_date\": \"2011-05-22T00:46:38\", \"slug\": \"another-post\", \"title\": \"Another Post\", \"user\": \"/api/v1/user/1/\"}" http://localhost:8000/api/v1/entry/