我一直在尝试使用 Python requests
在 api 上执行 POST 请求。 curl 命令似乎有效,但 Python requests
失败。
以下是 curl
代码:
curl --location --request POST '{{url}}' \
--header 'Content-Type: multipart/form-data' \
--header 'Accept: application/json;v=1' \
--header 'Md5-Checksum: {{checksum of file_to_upload}}' \
--header 'Authorization: {{auth-token}}' \
-F 'file=@{{path of file_to_upload}}'
以下是类似的 Python
代码:
import requests
headers = {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json;v=1',
'Md5-Checksum': f'{checksum of file_to_upload}',
'Authorization': f'{auth-token}',
}
files = {
'file': (f'{path of file_to_upload}', open(f'{path of file_to_upload}', 'rb')),
}
response = requests.post(f'{url}', headers=headers, files=files, verify=False)
然而这会引发错误:
{
"actions":[],
"id":"200000",
"text":"Our system experienced an error. Please try again later",
"developerText":"Request validation error",
"errorDetails":[{"id":"200000","text":null,"developerText":"HTTP 400 Bad Request"}]
}
谁能告诉我这两种方式有什么区别?