python3 request body with variable - python-3.x

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

Related

error 400 while running PUT request in python

I have a working curl command:
ip_address='<my ip address>'
sessionID='<got from post request metadata>'
curl --insecure --request PUT 'https://'$ip_address'/eds/api/context/records' -H 'Csrf-Token: nocheck' -H 'AuthToken:<token>' -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"sessionId": "'$session_ID'", "replace":false}'
The python script looks like below:
headers_put = {
'Csrf-Token': 'nocheck',
'AuthToken': '<my token here>',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
url_put = "https://" + ip_address + "/eds/api/context/records"
data = {
"sessionId":"<session id got from POST request metadata>",
"replace":"false"
}
response = requests.put(url_put, headers=headers_put, data=data, verify=False)
print(response)
Error message I get is:
<Response [400]>
b'Bad Request: Invalid Json'
Any idea on what I am doing wrong here? Any help is appreciated!
EDIT:
Can this error cause because of data:
print(data)
{'sessionId': '<session id received from post metadata>', 'replace': 'false'}
Http status 400 means ‘bad request’ as outlined here. You have omitted 1 request header (Csrf-token) in pyScript that was contained in your curl statement. Consider including that and retry the script.
If you still receive an error - you could try and extract (in your script) the actual text or.body of the 4xx response. It may be accessible from the text property of the python response object (you can confirm content-type) using response.apparent_encoding. Best wishes.
I found the solution:
response = requests.put(url_put, headers=headers_put, json=data, verify=False)
instead of data= we have to use json=

Converting a CURL into python script

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!

python request equivalent of following curl put command

what is python request equivalent of following curl put command.
curl --location --request PUT 'https://xxxx/ejbca/ejbca-rest-api/v1/certificate/CN=CompanyName Issuing CA1 - PoC/69108C91844E53258C646444E0FF0FB797349753/revoke?reason=KEY_COMPROMISE' \
--header 'Content-Type: application/json' \
--data-raw ''
Try:
import requests
headers = {
'Content-Type': 'application/json',
}
params = (
('reason', 'KEY_COMPROMISE'),
)
response = requests.get('https://xxxx/ejbca/ejbca-rest-api/v1/certificate/CN=CompanyName%20Issuing%20CA1%20-%20PoC/69108C91844E53258C646444E0FF0FB797349753/revoke', headers=headers, params=params)
#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# response = requests.get('https://xxxx/ejbca/ejbca-rest-api/v1/certificate/CN=CompanyName Issuing CA1 - PoC/69108C91844E53258C646444E0FF0FB797349753/revoke?reason=KEY_COMPROMISE', headers=headers)
There is a library which supports these convertion.
pip install uncurl
I was not using correct url pattern, now I am using that I am able to do the needful.
import requests
url = f'https://someserver.dv.local/ejbca/ejbca-rest-api/v1/certificate/CN=SomeCompany%20Name%20Issuing%20CA1%20-%20PoC/8188/revoke?reason=KEY_COMPROMISE'
response = requests.put(
headers={
'content-type': 'application/json'
},
verify=('cacertstruststore.pem'),
cert=('restapi-cert.pem', 'restapi-key.key')
)
json_resp = response.json()
print(json_resp)
Response
{'issuer_dn': 'CN=SomeCompany Name CA1 - PoC', 'serial_number': '8188', 'revocation_reason': 'KEY_COMPROMISE', 'revocation_date': '2020-04-14T18:06:33Z', 'message': 'Successfully revoked', 'revoked': True}

Why does my curl request work and my python3 request not work when calling a webservice?

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.

Ecobee API: Documentation is for curl not sure how to translate to Python

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)

Resources