1. 程式人生 > 其它 >python 關於request模組post方法上傳檔案報錯"the request was rejected because no multipart boundary was found"

python 關於request模組post方法上傳檔案報錯"the request was rejected because no multipart boundary was found"

python-關於request模組post方法上傳檔案報錯"the request was rejected because no multipart boundary was found"

報錯情景

在瀏覽器的控制檯檢視傳送檔案的Post請求,可以發現在請求頭中會有引數“Content-Type”,值為“multipart/form-data”

在對介面進行自測的時候,用requests傳送帶檔案的post請求,在請求頭中新增'Content-Type': 'multipart/form-data'會返回
{"timestamp":"2021-11-10T08:53:26.840+00:00","status":500,"error":"Internal Server Error","message":"","path":"/file"}

這個檢視後臺,發現報org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found的錯誤

這個時候我想到去請求頭新增一個 boundary,但是結果還是解決不了問題,後臺依舊報錯,獲取不到上傳的檔案

解決方法

不在headers裡面加content-type,去掉之後,再嘗試,發現後臺成功收到了傳送的檔案。應該是requests模組根據上傳檔案自動添加了請求頭中的內容,