我将代码从NodeJS移植到python3。 我想发布图像二进制数据和文本。 我是红色的urllib3用户指南,但我无法做到。 我该怎么做 ?谢谢。
的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" : "text"
}
],
"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 = self.create_request_body(text)
files = {
"file": (filePath, binary_data, 'image/jpeg'),
"": ("", request_body, "xxx")
}
resp = requests.post("http://xxx", files=files, headers=headers)
我收到500错误。