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"
}


你可能感兴趣的:(requests上传文件)