compare python requests with curl

473 views Asked by At

I am interfacing with an API using requests and requests_oauthlib.

I successfully authenticate and access all the GET methods of the API, but get error 500 with POST methods. For example:

r = oauth.post("https://api.timelyapp.com/1.0/1/clients", 
data={"client":{"name":"newclient", "color":"c697c0" }}, 
allow_redirects=False, headers={"Content-Type": "application/json"})

The issue is that I tested the same exact call with curl and it works correctly, here the curl code:

curl -v -X POST -H "Content-Type: application/json" -H "Authorization: Bearer XXXXXXXXXXXX" --data '{"client": { "name": "newclient", "color":"c697c0" }}' "https://api.timelyapp.com/1.0/1/clients"

how can I dig deeper in requests to compare its call with curl?

UPDATE:

Also, noticed that if I do not specify content type:

r = oauth.post("https://api.timelyapp.com/1.0/1/clients", 
data={"client":{"name":"newclient", "color":"c697c0" }}, 
allow_redirects=True)

I get instead a 302 with redirection to the site homepage, where I fetch the content of the page. In any case the new client is not added.

1

There are 1 answers

0
Tuan Anh Hoang-Vu On BEST ANSWER

You might want to try this instead:

data=json.dumps(payload)

From python-requests doc:

There are many times that you want to send data that is not form-encoded. If you pass in a string instead of a dict, that data will be posted directly.