I am trying to convert a Curl POST request into a python script, and i am not getting the desired output, please let me know what i am doing wrong here.
CURL request
curl -s -w '%{time_starttransfer}\n' --request POST \
--url http://localhost:81/kris/execute \
--header 'content-type: application/json' \
--data '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
This runs the uptime command in the node for which the ip is provided and returns a JSON output:
{"command":"uptime","output":["{\"body\":\" 17:30:06 up 60 days, 11:23, 1 user, load average: 0.00, 0.01, 0.05\\n\",\"host\":\"10.0.0.1\"}"]}0.668894
When i try to run the same with python, it fails and never gets the output
Code :
import urllib3
import json
http = urllib3.PoolManager()
payload = '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
encoded_data = json.dumps(payload)
resp = http.request(
'POST',
'http://localhost:81/kris/execute ',
body=encoded_data,
headers={'Content-Type': 'application/json'})
print(resp)
I would recommend you use the requests library. It's higher level than urllib and simpler to use. (For a list of reasons why it's awesome, see this answer.)
Plus it requires only minor changes to your code to work:
import requests
payload = '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
resp = requests.post(
'http://localhost:81/kris/execute',
data=payload,
headers={'Content-Type': 'application/json'})
print(resp.text)
Note that the method POST is the function instead of a parameter and that is uses the named param data instead of body. It also returns a Response object, so you have to access its text property to get the actual response content.
Also, you don't need to json.dumps your string. That function is used to convert Python objects to JSON strings. The string you're using is already valid JSON, so you should just send that directly.
Here is an online utility you can check out to convert curl requests to python code.
Curl to python converter
Another alternative is Postman application. There you will have the option to convert curls to code for various languages, in the code section.
It a very good practice to check if the api requests are working by running the curl in postman.
And for your case, here is the code using python requests library.
import requests
headers = {
'content-type': 'application/json',
}
data = '{"command":["uptime"], "iplist":["10.0.0.1"], "sudo":true}'
response = requests.post('http://localhost:81/kris/execute', headers=headers, data=data)
Hope that helps! Happy Coding!
Related
I am trying to pull data from github api using specific search query and put it into a .txt file. I am able to do so by through curl and shell but I need to do it in python which I am very unfamiliar with. I have seen the requests library
I've tried using this website https://curl.trillworks.com/ and using requests library but I can't seem to wrap my head around how to format the request.
curl -H "Authorization: token xxx" 'https://api.github.com' "https://github.com/api/v3/search/repositories?q=Evidence+locker+Seed+in:readme" > evidencelockerevidence.txt
The above code does exactly what I need it to do (passes GHE token, calls api, stores it in a file) I just need help converting to python please.
EDIT: Solution was
import requests
headers = {
'Authorization': 'token xxx',
}
url = 'https://github.ibm.com/api/v3/search/repositories?q=Evidence+locker+Seed+in:readme'
response = requests.get(url, headers=headers)
print(response)
print(response.text)
All -H tags need to be specified in the headers key, and the url is the first positional argument.
import requests
headers = {
'Authorization': 'Bearer bearer_token',
}
url = 'https://github.com/api/v3/search/repositories?q=Evidence+locker+Seed+in:readme'
response = request.get(url, headers=headers)
print(response)
print(response.text)
That handles getting the data, now you have to worry about writing it to a file, which you can do with the open built-in function:
with open('path/to/evidencelockerevidence.txt', 'w') as file:
file.write(response.text)
So I have two (what I think are) identical web service requests...one is from curl which works, and the other is in Python3.
I don't understand the difference between them because the curl one works and returns the expected result, and the Python3 one returns a 500 HTTP status code.
My curl call is as follows:
$ curl -d "{\"Service\": \"Activity\", \"Action\": \"GetDataHistory\",\"Data\": \"{'StartTime':'2019-08-20T13:00:00+00:00', 'EndTime':'2019-08-20T13:10:00+00:00','GameId':'XXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBBBB'}\"}" -v -H "Content-Type: application/json" -H "cache-control: no-cache" -H "username: someusrname" -H "password: $(echo $vlpasswd)" -X POST -k https://xxx.xxx.xxx.xxx/api/Service/Call | /c/Users/meacct/AppData/Local/Continuum/anaconda3/python -m json.tool
And my Python3 code that "should" do the same thing is here:
import json
import requests as req
import urllib3
endpoint = 'https://xxx.xxx.xxx.xxx/api/Service/Call'
h = {'Content-Type': 'application/json',
'cache-control': 'no-cache',
'username': 'someusrname',
'password':'das-passwd'
}
d = {'Service': 'Activity',
'Action': 'GetDataHistory',
'Data':
{
"StartTime":"2019-08-20T13:00:00+00:00",
"EndTime":"2019-08-20T13:10:00+00:00",
"GameId":"XXXXXXXX-YYYY-ZZZZ-AAAA-BBBBBBBBBBBB"
}
}
# Shut up urllib3's warnings about an invaild SSL Certy.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
session = req.Session()
try:
res = session.post(url=endpoint, headers=h, data=d, verify=False)
except req.exceptions.SSLError:
print("SSL Error")
print(res.status_code)
res.text
and the output is:
500
'{"Message":"An error has occurred."}'
Why would this be when it looks to be the exact same call?
As the curl request is successful, this indicates that there is something wrong in your Python request, as opposed to the server configuration.
Instead of tracking down the issue, I suggest you try using curlconverter, to generate the Python request directly from the curl request.
I stuck in my Python3 code when using requests to make HTTP POST requests. I need to put variable "PackageId" inside data and gets error:
{"meta":{"code":4015,"type":"Bad Request","message":"The value of `carrier_code` is invalid."},"data":[]}
My code is:
import requests
import json
PackageId = input("Package number:")
headers = {
'Content-Type': 'application/json',
'Trackingmore-Api-Key': 'MY-API-KEY',
}
data = {
'tracking_number': PackageId,
'carrier_code': 'dpd-poland'
}
request = requests.post('https://api.trackingmore.com/v2/trackings/post', headers=headers, data=data)
The HTTP POST method used is fine, becouse when I hardcode the PackageId in Body, request is successful.
data = '{ "tracking_number": "1234567890", "carrier_code": "dpd-poland" }'
What might be wrong? Please help, I stuck and spend many hours trying to find a problem.
Here is a CURL command I want to reproduce:
curl -XPOST -H 'Content-Type: application/json' -H 'Trackingmore-Api-Key: MY-API-KEY' -d '{ "tracking_number": "01234567890", "carrier_code": "dpd-polska" }' 'https://api.trackingmore.com/v2/trackings/post'
Thanks !!!
You need to convert the data dict to a json string when providing it to post(), it does not happen implicitly:
request = requests.post('https://api.trackingmore.com/v2/trackings/post', headers=headers, data=json.dumps(data))
I'm having trouble working with an api. I was given a password and the header (x-app-id) I was given little documentation on the api but below it is shown how to retrieve the data using curl (from documentation):
curl -X GET "https://r6db.com/api/v2/players?
name=Bomb&platform=ps4&exact=true" -H "accept: application/json" -H "x-app-
id: password"
Above returns information on a player named 'Bomb' on the platform 'ps4' relating to the game Rainbow six siege. exact = true means tat the name has to be exact.
Can't give password for security reasons. Here is my try at replicating above in python going off another post I saw
import json
import requests
URL = "https://r6db.com/api/v2/players?name=Bomb&platform=ps4&exact=true"
KEY = "password"
headers = {"x-app-id": "application/json"}
params = {
"a-app-id": KEY
}
resp = requests.post(URL + "login/", headers = headers,
data=json.dumps(params))
if resp.status_code != 200:
print('error: ' + str(resp.status_code))
else:
print('Success')
Needless to say it doesn't work (it outputs error 404) which probably means I haven't even managed to get x-app-id as the header, any help appreciated thanks.
I think you have made a bit of a mess when passing the parameters with the module requests
There are two things to pass in the headers: accept: application/json and x-app-id: password and you are doing that fine in curl.
But in python you are passing the header "x-app-id": "application/json" (makes not much sense) and try to pass "a-app-id": KEY} as a parameter, not as a header.
If you pass it as a parameter, it will be included in your url in the form of https://r6db.com/api/v2/players?name=Bomb&platform=ps4&exact=true&a-app-id=KEY and that is not what you want.
You already know how to pass headers in python, so you only have to pass the right ones: Pass accept: application/json and x-app-id: password
If I got your description right, I think you donĀ“t have to pass data parameters.
The Ecobee API documentation shows this as a way to access their API:
#curl -s -H 'Content-Type: text/json' -H 'Authorization: Bearer AUTH_TOKEN' 'https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"","includeRuntime":true\}\}'
I have used that code in curl and it seems to work.
However when I try what I think is the equivalent python code it doesn't work.
(I really don't know curl well at all. What I know I know from a few hours of internet research.)
the code I am using:
import requests
headers = {"Content-Type": "text/json", "Authorization": "Bearer AUTH_TOKEN"}
response = requests.get('https://api.ecobee.com/1/thermostat?format=json&body=\{"selection":\{"selectionType":"registered","selectionMatch":"","includeRuntime":"true"\}\}', headers=headers)
print(response.text)
When I send this I get:
{
"status": {
"code": 4,
"message": "Serialization error. Malformed json. Check your request and parameters are valid."
}
}
Not sure what could be wrong with my json formating. Any help is much appreciated.
You'll need to URL-escape the special characters in the parameters.
Doing this by hand can be messy and prone to mistakes. I'm not a Python expert but initial research suggests using the params option built into Python's request.get(). For example:
import requests
url = 'https://api.ecobee.com/1/thermostat'
TOKEN = 'ECOBEEAPIACCESSTOKEN'
header = {'Content-Type':'text/json', 'Authorization':'Bearer ' + TOKEN}
payload = {'json': '{"selection":{"selectionType":"registered","selectionMatch":"","includeRuntime":"true"}}'}
response = requests.get(url, params=payload, headers=header)
print(response.url)
print(response.text)