'Calling a REST API with Api Id and key using Python
I am trying to send a request to get a token to an API.
curl -X POST "https://base_url/token"
-H "accept: application/json"
-H "Content-Type: application/json" -d "{ "appId": some_id, "appKey": some_key}"
This is what I did but I keep getting 400 error
response:
import requests
headers = {'accept': 'application/json', "Content-Type": "application/json"}
method = "POST"
data = {
"appId": api_id,
"appKey": api_key
}
rsp = requests.request("POST", url, headers=headers, data=data)
This gives me a 400 error - Bad Request failed to read HTTP message
.
In addition, please can anyone explain what -H
and -d
mean? I guess -H
means headers?
Solution 1:[1]
You have to use json
parameter instead of data. It should contain A JSON serializable Python object to send in the body of the Request.
rsp = requests.request("POST", url, headers=headers, json=data)
Solution 2:[2]
you can send you request with .post
method if you want to send your payload as data:
rsp = requests.post(url, headers=headers, data=data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abdul Niyas P M |
Solution 2 | Javad Nikbakht |