I'm trying to turn this cURL request into a Python code. I want to eventually be able to save this to a CSV file but I need to get connected first.
curl --compressed -H 'Accept: application/json' -H 'X-Api-Key: 123abc' 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/cbse/spot/btc-usd/aggregations/count_ohlcv_vwap?interval=1h'
I started with this:
import requests
import json
key='api-key'
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
s = requests.Session()
s.auth = (key)
headers = {
*not sure how to do this*
}
r = requests.get(url, headers=headers)
The docs say this needs to be in the header:
Accept: application/json
Accept-Encoding: gzip:
How do I include the API key? how do I save the data once its returned?
X-Api-Key would be a request header, so you can include it in your headers variable, like this:
headers = {
"X-Api-Key": key,
"Accept": "application/json",
"Accept-Encoding": "gzip"
}
(took the others ones from your current curl request)
You can get the data by using r.text, like this:
print(r.text)
Your code should look like this:
import requests
import json
key='api-key'
url = 'https://us.market-api.kaiko.io/v2/data/trades.v1/exchanges/'
headers = {
"X-Api-Key": key,
"Accept": "application/json",
"Accept-Encoding": "gzip"
}
r = requests.get(url, headers=headers)
print(r.text)
If you want to get a json object instead, you can use r.json()
Related
I'm trying to write a api script via python. I want to access the URL "https://xxx/2.0/article" via "requests.get" so that it is displayed. If this was successful, I want to reprogram it to write all "articles" into a CSV.
I worked with curl first and it worked well
curl -X GET \
https://xxx/2.0/contact \
-H 'Accept: application/json' \
-H 'Authorization: Bearer {access-token}'
It also displayed the data directly. When I tried this with Python, it gave me an "Unauthorized".
import requests
newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.get('https://api.bexio.com/2.0/article', headers={'Authorization': 'newHeaders'
'Bearer '})
print(response.status_code)
print(response.text)
I don't know exactly what I have to do to make this work and how I can export/write this directly to a CSV.
Can anyone help me here?
Thanks!
I fixed it with the auth
#!/usr/bin/python
import requests
url = "https://api.bexio.com/2.0/article"
headers = {
'Accept': "application/json",
'Content-Type': "application/json",
'Authorization': "Bearer }
response = requests.request("GET", url, headers=headers)
print(response.text)
I saw my fault. The last question is, how can i get the output in a csv file? When I make an export of my stock in the frontend, I get a CSV with the fields "Product" "Code" "Storage location" Purchase value" "Stock" and "Minimum stock". But with the script comes of course much more output.
I'm facing an issue with the code below:
!curl -X POST \
-H 'Content-Type':'application/json' \
-d '{"data":[[4]]}' \
http://0.0.0.0/score
How can I convert this code into a Python function or using Postman?
import requests
payload = {
"data": [[4]]
}
headers = {
'Content-Type': "application/json",
}
server_url = 'http://0.0.0.0/score'
requests.post(server_url, json = payload, headers = headers)
should be roughly equivalent to your curl command.
Otherwise, to "translate" curl into Python commands you could use tools like https://curl.trillworks.com/#python.
Postman has a convenient "import" tool to import curl commands like yours (pasting your command as raw text).
The result can also be "exported" into Python code using Postman.
Shortest equivalent (with requests lib) will look like this:
import requests # pip install requests
r = requests.post("http://0.0.0.0/score", json={"data":[[4]]})
requests will automatically set appropriate Content-Type header for this request.
Note that there will still be some differences in request headers because curl and requests always set their own set of headers implicitly.
Your curl command will send this set of headers:
"Accept": "*/*",
"Content-Length": "8", # not the actual content length
"Content-Type": "application/json",
"Host": "httpbin.org", # for testing purposes
"User-Agent": "curl/7.47.0"
And requests headers will look like that:
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"Content-Length": "8",
"Accept": "*/*",
"Content-Type": "application/json"
So you can manually specify User-Agent header in headers= keyword argument if needed.
But compression will still be used.
I'm trying to convert the cURL to Python request but doesn't work.
cURL: curl -kv -H 'Content-Type: application/json' 'https://IP-address/api/v1/login' -d '{"username":"api", "password":"APIPassword"}'
My Python requests code:
import requests
url = "https://IP-address/api/v1/login"
payload = "'{\"username\":\"api\", \"password\":\"APIPassword\"}'"
headers = {
'Content-Type': "application/json",
'cache-control': "no-cache",
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False)
print(response.text)
Which doesn't work and gives me 400 bad requests error.
I tried converting using the https://curl.trillworks.com/
which gives me the following code which doesn't work either.
import requests
url = 'https://IP-address/api/v1/login'
headers = {
'Content-Type': 'application/json',
}
data = '{"username":"api", "password":"APIPassword"}'
output = requests.get(url, data=data, verify=False)
print (output)
Can anyone please help me identify the issue here.
Edit: I have edited 2nd script to produce output: Which gives 500 Error
Use the json parameter in requests.post for json data. It also takes care of the headers.
data = {"username":"api", "password":"APIPassword"}
response = requests.post(url, json=data, verify=False)
Another way to make sure you're sending valid JSON in your payload would be to use the json python library to format your payload via json.dumps(), which returns a string representing a json object from an object. This was especially useful to me when I needed to send a nested json object in my payload.
import json
import requests
url = 'https://sample-url.com'
headers = { 'Content-Type': 'application/json', 'Authorization': f'{auth_key}'}
payload = { "key": "value",
"key": ["v1", "v2"],
"key": {
"k": "v"
}
...
}
r = requests.post(url, headers=headers, data=json.dumps(payload))
I want to call CURL API on python.
curl -X POST -H "Authorization:Token 00d2e3a10c82420414b2d36d28fb5afc2cd8e8a5" \
-H "Content-Type: application/json" \
-d '{"module_id":"[MODULE_ID]", "text": "example text"}' \
-D - \
https://api.tocall.com/
I used requests module for making request and json module for converting object to string. But I'm getting 404.
Where am I wrong?
import requests
import json
headers = {
'Authorization': 'Token 00d2e3a10c82420414b2d36d28fb5afc2cd8e8a5',
'Content-Type': 'application/json',
}
url = "https://api.tocall.com/"
data = '{"module_id":"[MODULE_ID]", "text": "example text"}'
response= requests.post(url, data=json.dumps(data), headers=headers)
print(response.status_code)
You are encoding your data as JSON twice. json.dumps() takes an object and converts to JSON. In this case, you are converting a string to JSON. This should work better:
import requests
headers = {
'Authorization': 'Token 00d2e3a10c82420414b2d36d28fb5afc2cd8e8a5',
}
url = "https://api.tocall.com/"
data = {"module_id":"[MODULE_ID]", "text": "example text"}
response= requests.post(url, json=data, headers=headers)
print(response.status_code)
If it still doesn't work and you need more help, you should include real details about your API so we can reproduce the issue.
json.dumps turns a Python dict to a string, but your data is already a string. The easiest thing to do is write data as a dict then use json.dumps on that.
Add the Host header, so that the final server knows on which virtual host to route the request,
Change:
headers = {
'Authorization': 'Token 00d2e3a10c82420414b2d36d28fb5afc2cd8e8a5',
'Content-Type': 'application/json',
}
For:
headers = {
'Authorization': 'Token 00d2e3a10c82420414b2d36d28fb5afc2cd8e8a5',
'Content-Type': 'application/json',
'Host' : 'api.tocall.com'
}
I think this will fix your issue. Eventually you might want to update the default headers, not craft your own ones. Try to use the session features of requests to perform consistent queries.
Note: as stated by other answers, you have other JSON encoding issues, but that's not the reason why you are getting 404.
I am trying to access the API v2 from thetvdb.com. Unfortunately I always get the 403 error.
Here is what I have:
#!/usr/bin/python3
import requests
url = "https://api.thetvdb.com/login"
headers = {'content-type': 'application/json'}
payload = {"apikey":"123","username":"secretusername","userkey":"123"}
post = requests.post(url, data = payload, headers = headers)
print(post.status_code, post.reason)
According to the API documentation I have to authenticate in order to get a token. But I just get 403 Forbidden.
Now I tried it using curl:
curl -X POST --header 'Content-Type: application/json' --header 'Accept:
application/json' -d
{"apikey":"123","username":"secretusername","userkey":"123"}'
'https://api.thetvdb.com/login'
And this worked perfectly. Can anyone explain me what I am missing? This is driving me insane.
I also tried it with
post = requests.post(url, data = json.dumps(payload), headers = headers)
Same error.
You have to explicitly convert the payload to json string and pass asdata . It looks like you have done that also you may try setting the user-agent as curl/7.47.1
headers = {'content-type': 'application/json', 'User-Agent': 'curl/7.47.1'}
post = requests.post(url, data = json.dumps(payload), headers = headers)
The program will look like
#!/usr/bin/python3
import requests
import json
url = "https://api.thetvdb.com/login"
headers = {'content-type': 'application/json', 'User-Agent': 'curl/7.47.1'}
payload = {"apikey":"123","username":"secretusername","userkey":"123"}
post = requests.post(url, data = json.dumps(payload), headers = headers)
print(post.status_code, post.reason)
I think you need to pass Accept headers in the python requests. Something like this:
header = {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
"Accept-Encoding": "gzip, deflate, sdch, br",
"Accept-Language": "en-US,en;q=0.8",
"User-Agent": "some user-agent",
}
url = 'http://169.254.169.254/latest/meta-data/iam/'
payload = {}
headers = {'content-type': 'application/json', 'User-Agent': 'curl/7.47.1'}
#headers = {'content-type': 'application/json'}
response = requests.post(url, headers=headers, data=payload,
verify='/installed/aws/usr/lib/python2.7/site-
packages/certifi/cacert.pem', timeout=5)
instance_profile_role_name = response.text
print response.text`