2017-03-03 373 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