【发布时间】:2025-12-31 02:25:11
【问题描述】:
下面是我的代码。我正在尝试使用带有 REST API 的 python 进行 POST 操作。我有一张我想发布的图片。我收到错误提示;
"'code': 'BadRequest', 'message': "无法处理传入请求: '缺少内容类型边界。'。请确保它是 结构良好”
我在哪里犯错了?
import requests
headers = {
'accept': 'application/json',
'Content-Type': 'multipart/form-data',
#'boundary':'---BOUNDRY'
}
params = (
('returnFaceId', 'true'),
('returnFaceLandmarks', 'true'),
)
files = {
'form': (open('image.jpg', 'rb'),'image/jpg'),
}
response = requests.post('http://localhost:5000/face/v1.0/detect', headers=headers, params=params, files=files)
print (response.json())
【问题讨论】:
-
我建议阅读有关您发送请求的特定 REST API 上的图像上传的信息。可能是您必须使用 base64 对图像进行编码...
'form': (base64.encodestring(fobj.read()), 'image/jpg') -
我也强烈建议不要打开文件对象而不关闭它。改用上下文管理器...
with open('filename.ext', 'rb') as fobj: ...
标签: python rest python-requests