No JSON object could be decoded - tastypie - curl

nknj picture nknj · Jul 9, 2012 · Viewed 9.1k times · Source

I was following the tastypie tutorial word for word until i reached the post part: http://django-tastypie.readthedocs.org/en/latest/interacting.html#creating-a-new-resource-post

When i run this command i keep getting the following error: No JSON object could be decoded

I checked and I am certain that I am following the documentation word for word.

Thanks for your help

Answer

nknj picture nknj · Jul 10, 2012

Turned out to be a windows thing with cURL.

  1. The JSON data should be quoted with double quotes ("") instead of single quotes.
  2. All the double quotes in the json packet must be escaped with a backslash (\)

Eg: So, this:

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/

Should be:

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/