搜尋

首頁  >  問答  >  主體

http - python requests上传文件问题

headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:255
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryAAAuEGgBgQABXetS

Request Payload
------WebKitFormBoundaryAAAuEGgBgQABXetS
Content-Disposition: form-data; name="file"; filename="67.pdf"
Content-Type: application/pdf


------WebKitFormBoundaryAAAuEGgBgQABXetS--
files = {'Filename':'67.pdf',

        'file':('67.pdf',open(r'C:\Users\john\Desktop\67.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这个文件的内容么?

怪我咯怪我咯2788 天前408

全部回覆(1)我來回復

  • PHP中文网

    PHP中文网2017-04-18 10:36:04

    搞定了!
    files = {

        'file':open(r'C:\Users\john\Desktop.pdf','rb')

    }
    就行了

    回覆
    0
  • 取消回覆