No JSON object could be decoded - tastypie - curl - python

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

Turned out to be a windows thing with cURL.
The JSON data should be quoted with double quotes ("") instead of single quotes.
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/

Related

How to input array of strings with Flask + Swagger UI?

I have a flask api that automatically validates and generates Swagger documentation thanks to marshmallow package.
One of the API methods is a POST request that wants a specific id as a string and a list_of_ids as an array of strings. The GUI looks like this:
Whatever I insert, I always get the same reply:
So my question is how do you input such list (or array) or strings? Is there an equivalent curl for it?
Hey I created some example POST request similar to what you reqested in Postman and here is the result cURL:
curl --location --request POST 'http://localhost:5000/api/v1/add_articles_to_article_list' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": "42",
"list_of_ids": [
1,
2
]
}'

curl: could not resolve host

I am trying to send a POST/PUT requests with CMD (command prompt in windows 10) using these commands:
1st:
curl -iX PUT -H "Content-Type: application/json" -d \"{\"name\": \"test number 1\", \"description\": \"a website test \", \"category\": \"test\", \"release_date\": \"2017-10-08T01:01:00.776594Z\"}\" localhost:8000/home/search/3
2nd:
curl -iX PUT -H "Content-Type: application/json" -d "{"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}" localhost:8000/home/search/3
and using these commands I am getting a curl: could not resolve error, and when its done naming everything it couldn't resolve I get a server error status 500 I don't understand why its not working cause I tried these commands and they seemed to work before and they don't.
(I thought I had an error in my code and I tried the http request instead of curl and it worked fine with no problem getting the 200 OK status.).
If NOT on Windows, the answer above by #Necklondon with the single quotes should work.
If on Windows, however, you'll need to escape the double quotes within the JSON string.
curl -iX PUT -H "Content-Type: application/json" -d "{""name"": ""test number 1"",""description"": ""A website test"",""category"": ""test"",""release_date"": ""2017-10-08T01:01:00.776594Z""}" http://localhost:8000/home/search/3
In addition, to illustrate how much simpler the CURL command becomes when using a separate file for your JSON...
C:\Users\...\Desktop>TYPE somefile.json
{
"name": "test number 1",
"description": "A website test",
"category": "test",
"release_date": "2017-10-08T01:01:00.776594Z"
}
C:\Users\...\Desktop>curl -iX PUT -H "Content-Type: application/json" --data-binary #somefile.json http://localhost:8000/home/search/3
Watch out for double quotes insides double quotes:
curl -iX PUT -H "Content-Type: application/json" -d '{"name": "test number 1", "description": "A website test", "category": "test", "release_date": "2017-10-08T01:01:00.776594Z"}' localhost:8000/home/search/3

HTTP form-encoding payload with nested arrays

I'm working with a third-party HTTP API like this:
curl https://secure.phabricator.com/api/maniphest.edit \
-d "api.token=${API_TOKEN}" \
-d "transactions[0][type]=subscribers.add" \
-d "transactions[0][value][0]=${OWN_USER_PHID}" \
-d "transactions[0][value][1]=${ANOTHER_USER_PHID}"
Basically it's a POST request with a request body like this (if written in Python syntax):
{"api.token": "...", "transactions": [{"type": "...", "value": ["...", "..."]}]
You can see that transactions is a nested array.
How do I encode such payload in Python? I'm using requests to make the request if that matters.

Python: How to keep single quotes in list/json

I am creating a rest api in Python using flask and using curl command to test my function.
Curl Command:
$ curl -X POST
-H "Content-Type:application/json"
-d '{"keyword": "2''binders", "country": "xyz", "frequency": "1","url":"www.example.com"}' \
http://127.0.0.1:5000/google
Output:
'['New Request', u'**2binders**', u'xyz', u'www.example.com', u'1']'
As you can see I am passing inches in json 2''binders but in my list it is removing that.
Edit:
Python:
json = request.get_json(force=True)
print json
In your curl command, try using "keyword": "2\'\'binders". You need to escape the single quotes inside the -d parameter, because you're using single quotes around the -d parameter.
The shell is getting confused and thinking you are wanting string concatenation. E.g. the shell is seeing 'abc''def' and interpreting that as 'abcdef'.
If you try to json.dumps the dictionary itself:
>>> s = json.dumps(d)
>>>
>>> s
'{"url": "www.example.com", "country": "xyz", "frequency": "1", "keyword": "2\'\'binders"}'
^ ^
You will see that it will automatically escape ', as mentioned in the previous answer, escaping it should do the trick to you:
>>> json.loads(s)
{u'url': u'www.example.com', u'country': u'xyz', u'frequency': u'1', u'keyword': u"2''binders"} #Single quote is back
Got it!!!
curl -X POST -d '{"keyword": "2'\'''\''binders", "country": "xyz", "frequency": "1","url":"http://www.example.com/"}' http://127.0.0.1:5000/google -H "Content-Type:application/json"

What is the python requests equivalent of the following curl command

On ably.io they have an example where one can use the following curl request to publish a message to a channel:
curl -X POST https://rest.ably.io/channels/channelname/messages \
-u "some_AP.aYYMcQ:VmGHauKOqo-35Zxo" \
-H "Content-Type: application/json" \
--data '{ "name": "greeting", "data": "example" }'
The value passed to -u is an API key that has publish privileges. How does one make the same post request using Python requests library? I searched the documentation but could not find it. Note there is no password here, only the api key.
Thanks in advance.
You could use this:
requests.post("https://rest.ably.io/channels/channelname/messages",
auth=('some_AP.aYYMcQ', 'VmGHauKOqo-35Zxo'), # Equivalent of -u
json={ "name": "greeting", "data": "example" }) # Equivalent of --data
When you use the json option, -H is automatically set to Content-Type: application/json.

Categories