您的位置:首页技术文章
文章详情页

http - python requests上传文件问题

【字号: 日期:2022-07-30 15:09:47浏览:26作者:猪猪

问题描述

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 &#39;path&#39; of undefined<br> &nbsp; &nbsp;at /u/apps/jibe-apply-app_4.0.41_master/server/routes/api/apply/upload.js:96:33<br> &nbsp; &nbsp;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> &nbsp; &nbsp;at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:131:13)<br> &nbsp; &nbsp;at /u/apps/jibe-apply-app_4.0.41_master/server/middleware/statsd.js:40:7<br> &nbsp; &nbsp;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> &nbsp; &nbsp;at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:131:13)<br> &nbsp; &nbsp;at module.exports (/u/apps/jibe-apply-app_4.0.41_master/server/middleware/requireUser.js:6:5)<br> &nbsp; &nbsp;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> &nbsp; &nbsp;at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:131:13)<br> &nbsp; &nbsp;at Route.dispatch (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/route.js:112:3)<br> &nbsp; &nbsp;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> &nbsp; &nbsp;at /u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:277:22<br> &nbsp; &nbsp;at Function.process_params (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:330:12)<br> &nbsp; &nbsp;at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:271:10)<br> &nbsp; &nbsp;at expressInit (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/middleware/init.js:33:5)<br> &nbsp; &nbsp;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> &nbsp; &nbsp;at trim_prefix (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:312:13)<br> &nbsp; &nbsp;at /u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:280:7<br> &nbsp; &nbsp;at Function.process_params (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:330:12)<br> &nbsp; &nbsp;at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:271:10)<br> &nbsp; &nbsp;at query (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/middleware/query.js:44:5)<br> &nbsp; &nbsp;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> &nbsp; &nbsp;at trim_prefix (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:312:13)<br> &nbsp; &nbsp;at /u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:280:7<br> &nbsp; &nbsp;at Function.process_params (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:330:12)<br> &nbsp; &nbsp;at next (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:271:10)<br> &nbsp; &nbsp;at Function.handle (/u/apps/jibe-apply-app_4.0.41_master/node_modules/express/lib/router/index.js:176:3)<br> &nbsp; &nbsp;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’)

}就行了

标签: Python 编程
相关文章: