search

Home  >  Q&A  >  body text

How to upload files using Python's requests library?

<p>I am using Python's requests library to perform a simple task, which is to upload a file. I've searched on Stack Overflow and no one seems to be having the same problem, where the file is not being received by the server: </p> <pre class="brush:php;toolbar:false;">import requests url='http://nesssi.cacr.caltech.edu/cgi-bin/getmulticonedb_release2.cgi/post' files={'files': open('file.txt','rb')} values={'upload_file' : 'file.txt' , 'DB':'photcat' , 'OUT':'csv' , 'SHORT':'short'} r=requests.post(url,files=files,data=values)</pre> <p>I filled in the value of the 'upload_file' keyword with my filename because if I left it blank it would read: </p> <pre class="brush:php;toolbar:false;">Error - You must select a file to upload! </pre> <p>Now I get: </p> <pre class="brush:php;toolbar:false;">The size of file file.txt is bytes, uploaded successfully! Query service results: There are 0 rows in total. </pre> <p>This only occurs if the file is empty. So I don't know how to send my file successfully. I know the file is valid because if I fill out the form manually and visit the site, it returns a nice list of matches, which is exactly what I want. I really appreciate all the tips. </p> <p>Some other threads that are related (but don't solve my problem): </p> <ul> <li>Use a Python script to send files via POST</li> <li>http://docs.python-requests.org/en/latest/user/quickstart/#response-content</li> <li>Use requests to upload files and send additional data</li> <li>http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow</li> </ul><p><br /></p>
P粉071559609P粉071559609469 days ago626

reply all(2)I'll reply

  • P粉422227023

    P粉4222270232023-08-17 12:40:36

    (2018) The new Python requests library simplifies this process. We can use the 'files' variable to indicate that we want to upload a multi-part encoded file

    url = 'http://httpbin.org/post'
    files = {'file': open('report.xls', 'rb')}
    
    r = requests.post(url, files=files)
    r.text

    reply
    0
  • P粉590428357

    P粉5904283572023-08-17 11:32:38

    If upload_file refers to a file, use:

    files = {'upload_file': open('file.txt','rb')}
    values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}
    
    r = requests.post(url, files=files, data=values)

    Then requests will send a multipart form POST request body with the upload_file field set to the contents of the file.txt file.

    The file name will be included in the mime header of the specific field:

    >>> import requests
    >>> open('file.txt', 'wb')  # 创建一个空的演示文件
    <_io.BufferedWriter name='file.txt'>
    >>> files = {'upload_file': open('file.txt', 'rb')}
    >>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii'))
    --c226ce13d09842658ffbd31e0563c6bd
    Content-Disposition: form-data; name="upload_file"; filename="file.txt"
    
    
    --c226ce13d09842658ffbd31e0563c6bd--

    Please note the filename="file.txt"parameter.

    If you need more control, you can use tuples as files mapping values. The length of the tuple should be between 2 and 4. The first element is the file name, followed by the content, optionally including a mapping of the content-type header and other headers:

    files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}

    This will set an alternative filename and content type, omitting the optional headers.

    If you want the entire POST request body to come from a file (no other fields are specified), do not use the files parameter and POST the file directly as data. You may also want to set a Content-Type header, otherwise no header will be set. SeePython requests - POST data from a file.

    reply
    0
  • Cancelreply