在在线服务中,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 响应,这是我的代码直到现在
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)
有人能告诉我应该以哪种格式传递数据以获得良好的响应吗?
我通过不指定标头解决了我的疑问,它似乎是自动构建的,并且通过手动实现,您可能无法正确获取所有值,因为其中一些甚至是动态的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句