Home > Back-end >  Calling a REST API with Api Id and key using Python
Calling a REST API with Api Id and key using Python

Time:05-14

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?

CodePudding user response:

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)

CodePudding user response:

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