API调用在postman中工作,但当我使用生成的代码时,它不工作。
生成的代码:
import requests
url = "http://services.XXX.com/rest/v2/verification"
payload = "{\r\n \"startDate\": \"2000-12-25\",\r\n \"endDate\": \"2000-12-31\",\r\n \"format\": \"CSV\"\r\n}"
headers = {
'authorization': "Bearer XXXXX",
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "XXX"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)我尝试在删除postman-token后执行,但仍然出现以下错误:
{"message":"Internal Server Error: correlationId=V2-ee8fb1b098490b8665dd936e8472978b","type":"error","code":1}发布于 2021-07-01 14:02:23
在requests中使用传递json数据时,可以传递字典而不是字符串。
您可以尝试这样做:
import requests
import json
url = "http://services.XXX.com/rest/v2/verification"
payload = "{\r\n \"startDate\": \"2000-12-25\",\r\n \"endDate\": \"2000-12-31\",\r\n \"format\": \"CSV\"\r\n}"
headers = {
'authorization': "Bearer XXXXX",
'content-type': "application/json",
'cache-control': "no-cache",
'postman-token': "XXX"
}
response = requests.post(url,json=json.loads(payload),headers=headers)
print(response.text)发布于 2021-07-01 14:18:55
您可以通过以下方式将有效负载变量更改为Json oblect:
而不是:
payload = "{\r\n \"startDate\": \"2000-12-25\",\r\n \"endDate\": \"2000-12-31\",\r\n \"format\": \"CSV\"\r\n}"您可以使用:
payload = {"startDate": "2000-12-25", "endDate": "2000-12-31", "format":"CSV"}https://stackoverflow.com/questions/68203684
复制相似问题