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. docker-compose中volumes的问题2. debian - docker依赖的aufs-tools源码哪里可以找到啊?3. docker网络端口映射,没有方便点的操作方法么?4. PHP中的$this代表当前的类还是方法?5. docker不显示端口映射呢?6. angular.js - angular内容过长展开收起效果7. golang - 用IDE看docker源码时的小问题8. python的MySQLdb包rollback对create语句无效吗?9. html - 这种错位的时间轴怎么布局,然后用css实现?10. javascript - 用swiper.js实现h5多篇文章滑动 点击文章跳转全文是另外的页面 点击返回滑动界面怎么定位到相应的swiper_slide