http - python requests上傳文件問(wèn)題
問(wèn)題描述
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會(huì)報(bào)錯(cuò)
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里的內(nèi)容復(fù)制下來(lái),粘貼到某個(gè)文件,post這個(gè)文件,反而不會(huì)有這個(gè)錯(cuò)誤。requests不要post這個(gè)文件的內(nèi)容么?
問(wèn)題解答
回答1:搞定了!files = {
’file’:open(r’C:UsersjohnDesktop67.pdf’,’rb’)
}就行了
相關(guān)文章:
1. html5和Flash對(duì)抗是什么情況?2. 數(shù)據(jù)庫(kù) - Mysql的存儲(chǔ)過(guò)程真的是個(gè)坑!求助下面的存儲(chǔ)過(guò)程哪里錯(cuò)啦,實(shí)在是找不到哪里的問(wèn)題了。3. ios - 類(lèi)似微博首頁(yè),一張圖的時(shí)候是如何確定圖大小的?4. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)5. javascript - 在ie下為什么會(huì)出現(xiàn)這種情況呢 《 無(wú)法獲取未定義或 null 引用的屬性“l(fā)ength”》 ?請(qǐng)大神指教。6. javascript - 我是做web前端的,公司最近有一個(gè)項(xiàng)目關(guān)于數(shù)據(jù)統(tǒng)計(jì)的!7. javascript - 如何使用loadash對(duì)[object,object,object]形式的數(shù)組進(jìn)行比較8. css右浮動(dòng)字的順序顛倒了9. javascript - vue過(guò)渡效果 css過(guò)渡 類(lèi)名的先后順序10. javascript - vuejs+elementui 購(gòu)物車(chē)價(jià)格計(jì)算,點(diǎn)擊加減號(hào)修改數(shù)量總價(jià)都不會(huì)改變,但是計(jì)算執(zhí)行了
