python requests file upload

I'm performing a simple task of uploading a file using Python requests library. I searched Stack Overflow and no one seemed to have the same problem, namely, that the file is not received by the server:

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

I'm filling the value of 'upload_file' keyword with my filename, because if I leave it blank, it says

    Error - You must select a file to upload!

And now I get

    File  file.txt  of size    bytes is  uploaded successfully!
    Query service results:  There were 0 lines.

Which comes up only if the file is empty. So I'm stuck as to how to send my file successfully. I know that the file works because if I go to this website and manually fill in the form it returns a nice list of matched objects, which is what I'm after. I'd really appreciate all hints.

Some other threads related (but not answering my problem):

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.


Back to homepage or read more recommendations: