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模塊根據上傳文件自動添加了請求頭中的內容