How to upload file with python requests?

How to upload file with python requests?

If upload_file is meant to be the file, use:

files = {upload_file: open(file.txt,rb)}
values = {DB: photcat, OUT: csv, SHORT: short}

r =, files=files, data=values)

and requests will send a multi-part form POST body with the upload_file field set to the contents of the file.txt file.

The filename will be included in the mime header for the specific field:

>>> import requests
>>> open(file.txt, wb)  # create an empty demo file
<_io.BufferedWriter name=file.txt>
>>> files = {upload_file: open(file.txt, rb)}
>>> print(requests.Request(POST,, files=files).prepare().body.decode(ascii))
Content-Disposition: form-data; name=upload_file; filename=file.txt


Note the filename=file.txt parameter.

You can use a tuple for the files mapping value, with between 2 and 4 elements, if you need more control. The first element is the filename, followed by the contents, and an optional content-type header value and an optional mapping of additional headers:

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

This sets an alternative filename and content type, leaving out the optional headers.

If you are meaning the whole POST body to be taken from a file (with no other fields specified), then dont use the files parameter, just post the file directly as data. You then may want to set a Content-Type header too, as none will be set otherwise. See Python requests – POST data from a file.

(2018) the new python requests library has simplified this process, we can use the files variable to signal that we want to upload a multipart-encoded file

url =
files = {file: open(report.xls, rb)}

r =, files=files)

How to upload file with python requests?

Client Upload

If you want to upload a single file with Python requests library, then requests lib supports streaming uploads, which allow you to send large files or streams without reading into memory.

with open(massive-body, rb) as f:, data=f)

Server Side

Then store the file on the side such that save the stream into file without loading into the memory. Following is an example with using Flask file uploads.

@app.route(/upload, methods=[POST])
def upload_file():
    from werkzeug.datastructures import FileStorage
    FileStorage([UPLOAD_FOLDER], filename))
    return OK, 200

Or use werkzeug Form Data Parsing as mentioned in a fix for the issue of large file uploads eating up memory in order to avoid using memory inefficiently on large files upload (s.t. 22 GiB file in ~60 seconds. Memory usage is constant at about 13 MiB.).

@app.route(/upload, methods=[POST])
def upload_file():
    def custom_stream_factory(total_content_length, filename, content_type, content_length=None):
        import tempfile
        tmpfile = tempfile.NamedTemporaryFile(wb+, prefix=flaskapp, receiving file ... filename =>  + str(
        return tmpfile

    import werkzeug, flask
    stream, form, files = werkzeug.formparser.parse_form_data(flask.request.environ, stream_factory=custom_stream_factory)
    for fil in files.values(): .join([saved form name,, submitted as, fil.filename, to temporary file,]))
        # Do whatever with stored file at ``
    return OK, 200

Leave a Reply

Your email address will not be published. Required fields are marked *