我无法收到我的发帖请求的回复。它给我400错误。在邮递员中,效果很好。
代码在下面
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)
这是我得到的结果。
400
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Bad Request</pre>
</body>
</html>
谢谢!
答案 0 :(得分:1)
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)
您是否尝试过此操作,而不是使用字符串化的JSON对象作为有效负载?
答案 1 :(得分:0)
您的headers
JSON不正确。密钥带有单引号。正确的JSON要求双引号"key":"value"
,除非值是整数!尝试将所有引号更改为标题中的双引号。
您的有效载荷也很混乱
payload = """[{{\n \"dateOfBirth\": \"{}\",\n \"nationalIdentityNo\": \"{}\"\n}}]"""
只需直接在您的回复中注明
data='{{"dateOfbirth": {},"nationalIdentityNo": {}}}'.format(new_date, IDNO)