2017-03-03 296 views
0

在在线服务中,javascript正在使用新的表单数据来获取图像,然后通过简单的api调用通过此代码检索对象。python 400错误请求POST请求

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:错误的请求的响应,这是我的代码到目前为止

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) 

有人能告诉我以哪种方式我应该通过数据获得良好的回应?

回答

0

我解决了我的疑问不指定标题,似乎自动建立,并与手工执行你可能不能够得到所有值正确,因为其中一些甚至是动态的。

0

这可能是您用来访问网页的用户代理。在你的头变量使用此头而不是:

headers = {"Content-Type":"multipart/form-data","User-Agent" : "Mozilla/5.0"} 
+0

我试着按建议做,但没有改变,我改变了一些帖子的格式,以尽可能相似的http身体请求,但我其实没有文档 – Lukepell