我正在将代码从 NodeJS 移植到 python3。
我想发布图像二进制数据和文本。
我该怎么做 ?谢谢。
NodeJS
filePath = "xxx.jpeg"
text = "xxx"
return chakram.request("POST", "http://xxx",
{ "multipart" : [
{ "body" : fs.createReadStream(filePath),
"Content-Type" : "image/jpeg",
"Content-Disposition" : "name='file'; filename='" + filePath + "'"
},
{ "body" : JSON.stringify(this.RequestData(text)),
"Content-Type" : "application/specified-content-type-xxx"
}
],
"headers" : {
"Authorization" : "xxx"
}
})
我的错误Python代码requests
:
filePath = "xxx.jpeg"
text = "xxx"
headers = {
"Authorization" : "xxx"
}
binary_data = None
with open(file_path, 'rb') as fp:
binary_data = fp.read()
request_body = {
"text": text,
"type": "message",
"from": {
"id": "user1"
}
}
files = {
"file": (filePath, binary_data, 'image/jpeg'),
"": ("", request_body, "application/specified-content-type-xxx")
}
resp = requests.post("http://xxx", files=files, headers=headers)
我收到 500 错误。