http - python requests上傳文件問題
問題描述
headersAccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding:gzip, deflate, brAccept-Language:zh-CN,zh;q=0.8Cache-Control:max-age=0Connection:keep-aliveContent-Length:255Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryAAAuEGgBgQABXetSRequest Payload------WebKitFormBoundaryAAAuEGgBgQABXetSContent-Disposition: form-data; name='file'; filename='67.pdf'Content-Type: application/pdf------WebKitFormBoundaryAAAuEGgBgQABXetS--
files = {’Filename’:’67.pdf’,’file’:(’67.pdf’,open(r’C:UsersjohnDesktop67.pdf’,’rb’),’application/octet-stream’),’Upload’:’Submit Query’}html=requests.post(url=url,headers=headers,files=files)print (html.status_code)print (html.text)
像這樣post會報錯
TypeError: Cannot read property 'path' of undefined<br> at /u/apps/jibe-apply-app_4.0.41_master/server/routes/api/apply/upload.js:96:33<br> at Layer.handle [as handle_request] (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/layer.js:95:5)<br> at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:131:13)<br> at /u/apps/jibe-apply-app_4.0.41_master/server/middleware/statsd.js:40:7<br> at Layer.handle [as handle_request] (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/layer.js:95:5)<br> at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:131:13)<br> at module.exports (/u/apps/jibe-apply-app_4.0.41_master/server/middleware/requireUser.js:6:5)<br> at Layer.handle [as handle_request] (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/layer.js:95:5)<br> at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:131:13)<br> at Route.dispatch (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:112:3)<br> at Layer.handle [as handle_request] (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/layer.js:95:5)<br> at /u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:277:22<br> at Function.process_params (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:330:12)<br> at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:271:10)<br> at expressInit (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/middleware/init.js:33:5)<br> at Layer.handle [as handle_request] (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/layer.js:95:5)<br> at trim_prefix (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:312:13)<br> at /u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:280:7<br> at Function.process_params (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:330:12)<br> at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:271:10)<br> at query (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/middleware/query.js:44:5)<br> at Layer.handle [as handle_request] (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/layer.js:95:5)<br> at trim_prefix (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:312:13)<br> at /u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:280:7<br> at Function.process_params (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:330:12)<br> at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:271:10)<br> at Function.handle (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:176:3)<br> at EventEmitter.handle (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/application.js:173:10)
但是如果把payload里的內容復制下來,粘貼到某個文件,post這個文件,反而不會有這個錯誤。requests不要post這個文件的內容么?
問題解答
回答1:搞定了!files = {
’file’:open(r’C:UsersjohnDesktop67.pdf’,’rb’)
}就行了
相關文章:
1. MySQL中無法修改字段名的疑問2. angular.js - angularjs的自定義過濾器如何給文字加顏色?3. angular.js - angular內容過長展開收起效果4. javascript - 如何讓移動端網頁的輸入框固定在底部?5. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應6. Matlab和Python編程相似嗎,有兩種都學過的人可以說說嗎7. javascript - 微信小程序封裝定位問題(封裝異步并可能多次請求)8. 大家好,請問在python腳本中怎么用virtualenv激活指定的環境?9. python的前景到底有大?如果不考慮數據挖掘,機器學習這塊?10. javascript - 微信小程序限制加載個數
