I am new to Python. I am using 'requests' library to send post request. I am able to send post request without headers but when giving headers I am getting HTTP 400 error.
import requests
API_ENDPOINT = "https://reqres.in/api/users"
data = {"name": "Name1", "job": "job1"}
headers = {'Content-Type': 'application/json'}
# sending post request and saving response as response object
full_output = requests.post(url = API_ENDPOINT, headers=headers, data = data)
print("response status", full_output.status_code)
print("response", full_output.text)
Output:
response status 400
response <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request</pre>
</body>
</html>
It is working if I pass empty headers in the code
headers = {}
Not sure why it is not working. Request your help. Thanks.
json=dataor you need to dump yourdatadict to JSON first before if you use that json string withdata=. Note thatjson=datawill set the headersContent-Typefor you, so you can skip that as well.