0

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?

1
  • yes thats where I go the error message Bad Request failed to read HTTP message May 13, 2022 at 6:50

2 Answers 2

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)
2
  • do you also have a reference where I can find all these short forms like -H, -d, -u etc? May 13, 2022 at 6:55
  • 1
    @JA-pythonista Yes, You can find it on curl documentation here
    – user459872
    May 13, 2022 at 6:56
1

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.