Reputation: 205
I am unable to get the response of my post request. It gives me 400 error. while in postman, it works fine.
The code is below
import requests
from urllib3.exceptions import InsecureRequestWarning
url = ""
payload = """[{{\n \"dateOfBirth\": \"{}\",\n \"nationalIdentityNo\": \"{}\"\n}}]"""
headers = {
'x-req-id': "89567890987610",
'x-channel-id': "MB",
'x-sub-channel-id': "MB",
'x-country-code': "PK",
'x-customer-type': "C",
'Authorization': "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJhZGlsIn0.mRSZXF0glqRPyo2h15jHd51JwCeEnSUIBmYuTaAzmrQ",
'accept': "application/json",
'Content-Type': "application/json",
'User-Agent': "PostmanRuntime/7.19.0",
'Cache-Control': "no-cache",
'Postman-Token': "566b2e63-9320-4a59-9524-f480f33fd62f,f7ba71fd-503a-451c-8817-2198f09c1d0c",
'Host': "*********",
'Accept-Encoding': "gzip, deflate",
'Content-Length': "75",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
response = requests.request("POST", url, data=payload.format(new_date, IDNO), headers=headers, verify=False)
print(response.status_code)
print(response.text)
This is result I am getting.
400
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request</pre>
</body>
</html>
Thanks in advance!
Upvotes: 1
Views: 5983
Reputation: 3000
Your headers
JSON is incorrect. The key has single quotes. Correct JSON requires "key":"value"
in double quotes, unless a value is an integer! Try changing all quotes to double quotes in your header.
Also your payload is quite confusing
payload = """[{{\n \"dateOfBirth\": \"{}\",\n \"nationalIdentityNo\": \"{}\"\n}}]"""
Just make this in your response directly
data='{{"dateOfbirth": {},"nationalIdentityNo": {}}}'.format(new_date, IDNO)
Upvotes: 1
Reputation: 2087
https://www.w3schools.com/python/ref_requests_post.asp
url = "string"
payload = [{'key':'value'},] # a JSON object
headers = {'key':'value'} # a dictionary
response = requests.post(url, data=payload, headers=headers)
Did you try this, instead of using a stringified JSON object as payload?
Upvotes: 3