How to convert curl to python request (crushftp) - python

I would like to use crushftp api to set password for a user.
I'm using update user API
https://www..com/crush9wiki/Wiki.jsp?page=API
It has something like in the Curl
--data-urlencode
> 'user=<?xml+version="1.0"+encoding="UTF-8"?>+<user+type="properties"><password>thisismypass</password></user>'
curl -d command=setUserItem -d data_action=update -d xmlItem=user -d serverGroup=MainUsers -d username=curl_user --data-urlencode 'user=<?xml+version="1.0"+encoding="UTF-8"?>+<user+type="properties"><password>thisismypass</password></user>' http://crushadmin:pass#127.0.0.1:8080/
I try to convert:
import requests
url = "http://crushadmin:pass#127.0.0.1:8080/"
payload = "command=setUserItem&data_action=update&xmlItem=user&serverGroup=MainUsers&username=curl_user"
headers = {
'cache-control': "no-cache",
'Postman-Token': "9b1c470d-889a-4647-a0f1-4b2a5c00bf68"
}
response = requests.request("GET", url, data=payload, headers=headers)
print(response.text)
However, i don't know how to put xlm including password needed to change
user=+thisismypass

import requests
url = 'http://crushadmin:pass#127.0.0.1:8080/'
payload = {
'command': 'setUserItem',
'data_action': 'update',
'xx': 'xx'}
headers = {
'cache-control': 'no-cache',
'Postman-Token': '9b1c470d-889a-4647-a0f1-4b2a5c00bf68'}
response = requests.get(url=url, params=payload, headers=headers)
print(response)

Related

How to convert specific CURL request to Python

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)

Converting cURL request to Python

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()

Post request to specific API with file

I'm trying to make a post request to API:
access_token = '71e89e8af02206575b3b4ae80bf35b6386fe3085af3d4085cbc7b43505084482'
url_base = "https://dev-100-api.huntflow.ru/"
url_resume = '{0}account/6/upload'.format(url_base)
headers_resume = {
'Content-Type': 'multipart/form-data',
'X-File-Parse': 'true',
'Authorization': 'Bearer {0}'.format(access_token),
}
files = {
'file': open('Глибин Виталий Николаевич.doc', 'rb'),
}
resume1 = requests.post(url_resume, headers=headers_resume, files=files)
print(resume1.text)
And output is: {"errors": [{"type": "server_error"}]}
The documentation says:
POST /account/{account_id}/upload
To upload a file send a request multipart/form-data with a file in parameter file.
To make sure that the file will be processed by the system of field recognition, one has to pass a header X-File-Parse: true. In this case the response will contain the fields text, photo, fields.
Example request:
curl -X POST \
-H "Content-Type: multipart/form-data" \
-H "X-File-Parse: true" \
-H "Authorization: Bearer <token>" \
-F "file=#resume.pdf" \
https://api.huntflow.ru/account/<account_id>/upload
Help me, please! What's wrong?
After some research I found this issue related to the same website you're trying to send file to it
The solution is something like follows:
import requests
import os
import mimetypes
token = "71e89e8af02206575b3b4ae80bf35b6386fe3085af3d4085cbc7b43505084482"
url = "https://dev-100-api.huntflow.ru/account/6/upload"
def upload_file(name, content):
mimetypes.init()
ext_data = os.path.splitext(name)
if len(ext_data) > 1:
mime_type = mimetypes.types_map.get(ext_data[len(ext_data) - 1]) or 'application/zip'
else:
mime_type = 'application/zip'
headers = {
'Authorization': 'Bearer {}'.format(token),
'User-Agent': 'App/1.0 (eeee#eeee.ru)',
}
r = requests.request(
'POST',
url,
files={'file': (name, content, mime_type)},
headers=headers,
)
return r.json()
print(upload_file("test.txt", "text/plain"))
I got the following response:
{'name': 'test.txt', 'url': 'https://dev-100-store.huntflow.ru/uploads/named/z/q/j/zqj0jf98dud8grbazjleyjy3ii4wrsah.txt/test.txt?s=z-pORM3y1qUVU4XhbsHKBg&e=1589632103', 'content_type': 'text/plain', 'id': 58}

Why does Bearer not work in requests authorization header?

I'm having a problem sending an authorization token with Bearer to NEST API via python requests:
curl https://developer-api.nest.com -H "Authorization: Bearer c.123"
-H "Content-Type: application/json"
works fine, however:
nest_url = "https://developer-api.nest.com"
headers = {'Authorization': str('Bearer ' + token), 'Content-type': 'application/json'}
print(headers)
nest_data_req = requests.get(nest_url, headers=headers)
which prints as:
{'Content-type': 'application/json', 'Authorization': 'Bearer c.123'}
fails with a 401 unauthorized, as far as I can tell they are trying to make the same request so why does curl work (and postman for that matter) and python requests fail?
The following image shows the same working in postman:
Update:
So this code works 1 in 10 times (the other 9+ give me 401 unauthorized):
url = "https://developer-api.nest.com/"
auth_t = token.encode("ascii", "ignore")
headers = {
'authorization': "Bearer " + auth_t,
'content-type': "application/json",
'cache-control': "no-cache",
}
response = requests.request("GET", url, headers=headers)
print(response.text)
if I press submit in postman it works everytime without fail.
Turns this is a result of nest's API redirecting so you could consider this either a bug - as headers are removed from the redirected request, and headers should be on the session. Or a 'feature' as they were trying to resolve a CVE.
So here is a prototyped way to handle this:
def get_nest_data(token):
url = "https://developer-api.nest.com/"
auth_t = token.encode("ascii", "ignore")
headers = {
'authorization': "Bearer " + auth_t,
'content-type': "application/json",
}
try:
init_res = requests.get('https://developer-api.nest.com', headers=headers, allow_redirects=False)
if init_res.status_code == 307:
api_response = requests.get(init_res.headers['Location'], headers=headers, allow_redirects=False)
if api_response.status_code == 200:
return api_response.json()
elif init_res.status_code == 200:
return init_res.json()
except Exception as ce:
print(ce)

Error while accessing API v2 from thetvdb.com [Python requests. Error 403 ]

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`

Categories