【发布时间】:2017-03-03 18:11:27
【问题描述】:
在在线服务中,javascript 使用新的表单数据来获取图像,然后通过一个简单的 api 调用使用此代码检索对象。
function sendSearchImage(form) {
console.log(">>> sendSearchImage");
var image = new FormData();
image.append('refImage', readFile);
$.ajax({
type: 'post',
url: instore_home + '/products/query',
contentType: false,
processData: false,
data: image,
success: function (response) {
console.log("<<< " + JSON.stringify(response));
showQueryResults(response);
//alert("Ref image successfully added: code " + response.refImgId);
//alert(JSON.stringify(response));
//window.location.reload();
}
});
return false;
}
我想改为在 python 中调用相同的 API,但我不断收到 400:Bad Request response,这是我到目前为止的代码
files = {'refImage': ('1.jpg', open(os.getcwd()+"/images/1.jpg", "rb"), 'image/jpeg')}
headers = {"Content-Type":"multipart/form-data", "User-Agent" : "Mozilla/5.0"}
r = requests.post('Url', headers=headers, data=files)
谁能告诉我应该以哪种格式传递数据以获得良好的响应?
【问题讨论】:
标签: javascript python api post request