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?
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:
Python requests - print entire http request (raw)?
How can I see the request headers made by curl when sending a request to the server?
Related
I'm trying to code the following cURL API request in Python:
curl -X POST 'https://api.livecoinwatch.com/coins/list' \
-H 'content-type: application/json' \
-H 'x-api-key: <YOUR_API_KEY>' \
-d '{"currency":"USD","sort":"rank","order":"ascending","offset":0,"limit":2,"meta":false}'
I tried solving it with guidance of another post, like this:
headers = {
'x-api-key': <YOUR_API_KEY>,
'content-type': 'application/json',
'host': https://api.livecoinwatch.com/coins/list
}
url = https://api.livecoinwatch.com/coins/list
data = '{"currency": "USD","sort": "rank","order": "ascending","offset": 0,"limit": 50,"meta": true}'
response = requests.post(url, data=json.dumps(data), headers=headers)
print (response)
Unfortunately I get a "bad request" error.
Can someone please help me where I go wrong?
Assuming you have your urls wrapped in quotes, you should try giving to the data function parameter a dictionary instead of a string as the requests documentation says: data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.
response = requests.post(url, data=json.loads(data), headers=headers)
Id like to convert this curl command to python script
curl -v -H "Content-Type: application/json" -H "Authorization: Bearer MYTOKEN" https://www.zopim.com/api/v2/chats
I wrote the python script below but I get <Response [400]>and doesn't work.
import requests
url = "https://www.zopim.com/api/v2/chats"
access_token = "MYTOKEN"
response = requests.post(url,
headers={"Content-Type":"application/json;charset=UTF-8",
"Authorization": "Bearer {}".format(access_token)})
print(response)
Any advice will be appreciated.thanks.
You should be using requests.get instead of requests.post, since what you want is a GET request:
import requests
url = "https://www.zopim.com/api/v2/chats"
access_token = "MYTOKEN"
response = requests.get(url,
headers={"Content-Type":"application/json;charset=UTF-8",
"Authorization": "Bearer {}".format(access_token)})
print(response)
I have the following Curl request:
curl -v --location --request POST 'http://127.0.0.1:8080/abc' \--header 'Content-Type: application/json' \--data-raw '{data}'
I tried using pycurl and requests command.
Also tried to put headers but it was of no use.
My tried code:
requests = "curl -v --location"
a = "http://127.0.0.1:8080/abc"
headers = {'Content-Type': 'application/json'}
r = requests.post(url=a, headers= headers , params=data)
Is this works?
https://curl.trillworks.com/
import requests
headers = {
'Content-Type': 'application/json',
}
data = '{data}'
response = requests.post('http://127.0.0.1:8080/abc',
headers=headers,
data=data)
How can I send this curl out using Python?
I did find similar requests, but cannot adapt my code to suite the query
I have also tried using pycurl, following this example but without luck.
$ curl -v -X GET -H "Accept: text/csv" -H "Authorization: Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==" "https://epc.opendatacommunities.org/api/v1/domestic/search"
Thanks
If you are using the Python Requests package the following code snippet should work:
import requests
headers = {
'Accept': 'text/csv',
'Authorization': 'Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==',
}
response = requests.get('https://epc.opendatacommunities.org/api/v1/domestic/search', headers=headers)
response.status_code # 200
response.text # "lmk-key,address1,address2,address3,postcode,buildi ..."
(Note: I used the https://curl.trillworks.com/ website to automatically make the conversion)
Have a CURL request like that:
curl -X POST "https://page.com/login"
-H "accept: application/json" -H "Content-Type: application/json"
-d "{ \"username\": \"admin\", \"password\": \"pass\"}"
In Python I guess it should look like this:
import requests
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
data = {'username': 'admin', 'password': 'pass'}
response = requests.post('https://page.com/login', headers=headers, data=data)
response
After this it gives me [502] error for bad gateway. What am I doing wrong with my python query and how it should be modified?
Try using:
requests.post(..., json=data)
When you use data= requests will send it form encoded, to actually put json in the body you have to use json=