1

I'm trying to grab some data from a website using API, but I'm having trouble converting the example curl command to python requests.

example curl command

curl -X POST "some_url" \
-H "accept: application/json" \
-H "Authorization: <accesstoken>" \
-d @- <<BODY 
{} 
BODY

My python requests that didn't work

headers = {
    'Authorization': "Bearer {0}".format(access_token)
}

response = requests.request('GET', "some_url", 
                            headers=headers, allow_redirects=False)

I get error code 400, can anyone help me figure out what was wrong?

efsee
  • 579
  • 1
  • 10
  • 22
  • 2
    Your cURL request mentions POST but you're making a GET with the python code, any reason for that? – razdi Aug 13 '19 at 23:36
  • @razdi I tried with POST too but didn't work, I'm not familiar with the curl format so I just tried things out. Can you show me the correct format in python given this curl command? – efsee Aug 13 '19 at 23:41
  • This might not be the solution but the tool might actually get you close: https://curl.trillworks.com/ – razdi Aug 13 '19 at 23:45
  • what type of data is `BODY`? – Ivan Vinogradov Aug 14 '19 at 13:09

1 Answers1

0

The equivalent requests code for your curl should be:

import requests

headers = {
    'accept': 'application/json',
    'Authorization': '<accesstoken>',
}

data = "{} "
response = requests.post('http://some_url', headers=headers, data=data)

You can use https://curl.trillworks.com/ to convert your actual curl invocation (note that it won't handle heredocs, as in your example).

If you see different behavior between curl and your python code, dump the HTTP requests and compare:

Nickolay
  • 31,095
  • 13
  • 107
  • 185