requests上传文件

很久没看requests了,浏览了下官方文档

import requests
url = 'http://httpbin.org/post'
#files with defaultname with params
# files = {'file': ('report11.xls', open('inventory_detail.xlsx', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
#make string to be file
files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}

r = requests.post(url, files=files)
print r.text

files with defaultname with params

{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "data:application/vnd.ms-excel;base64,UEsDBBQABgAIAAAAIQBKc9LYbwEAACgGAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAZG9jUHJvcHMvYXBwLnhtbFBLBQYAAAAADQANAFIDAABjJAAAAAA="
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "10387", 
    "Content-Type": "multipart/form-data; boundary=ba74c1d92ef64feb9186ffaad249e144", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.17.3"
  }, 
  "json": null, 
  "origin": "210.176.98.222", 
  "url": "http://httpbin.org/post"
}

make string to be file

{
  "args": {}, 
  "data": "", 
  "files": {
    "file": "some,data,to,send\nanother,row,to,send\n"
  }, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "184", 
    "Content-Type": "multipart/form-data; boundary=e7e9e37ed0d348219932a63e95e89188", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.17.3"
  }, 
  "json": null, 
  "origin": "210.176.98.222", 
  "url": "http://httpbin.org/post"
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容