【发布时间】:2021-12-02 06:22:00
【问题描述】:
我有一个脚本调用 POST 端点但收到 400 错误。同时对应的cURL请求成功。
首先,这里是 cURL:
curl -X 'POST' \
'http://localhost:8080/api/predict?Key=123testkey' \
-H 'accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F 'file=@156ac81cde4b3f22faa4055b53867f38.jpg;type=image/jpeg'
并翻译为请求:
import requests
url = 'http://localhost:8080/api/predict?Key=123testkey'
headers = {
'accept': 'application/json',
'Content-Type': 'multipart/form-data',
}
params = {'Key' : '123testkey'}
files = {'image': open('156ac81cde4b3f22faa4055b53867f38.jpg', 'rb')}
response = requests.post(url, files=files, params=params, headers=headers)
也尝试使用不包含密钥的 URL,因为密钥已在参数中指定:
import requests
url = 'http://localhost:8080/api/predict'
headers = {
'accept': 'application/json',
'Content-Type': 'multipart/form-data',
}
params = {'Key' : '123testkey'}
files = {'image': open('156ac81cde4b3f22faa4055b53867f38.jpg', 'rb')}
response = requests.post(url, files=files, params=params, headers=headers)
我认为这应该很简单,但无论我尝试什么,我都会收到 400 错误的请求。有什么建议吗?
编辑:也尝试了 'image/jpeg' 而不是 'image' 无济于事。
编辑:不幸的是,用“文件”替换“图像”键也不起作用
编辑:它在邮递员桌面上工作得很好,并生成以下代码。但是,此代码也会引发错误。
邮递员生成的代码:
import requests
url = "http://localhost:8080/api/predict?Key=123test"
payload={}
files=[
('file',('images19.jpg',open('156ac81cde4b3f22faa4055b53867f38.jpg','rb'),'image/jpeg'))
]
headers = {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
还有来自邮递员之前生成的代码的错误:
{"detail":"There was an error parsing the body"}
任何帮助弄清楚发生了什么将不胜感激!
【问题讨论】:
-
在
files中,键“image”的值是文件描述符,而不是数据。您需要读取和编码(可能是 Base64)该数据。 -
尝试使用邮递员 - stackoverflow.com/questions/39037049/…。一旦它在那里工作 - 让邮递员为你生成 python 代码。
-
感谢您的建议,我让它在邮递员中正常工作,但生成的代码抛出以下错误:{“detail”:“解析正文时出错”}。
标签: python rest http curl python-requests