python requests file upload

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


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
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)



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):




1 Answer
1



If upload_file is meant to be the file, use:


upload_file


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

r = requests.post(url, 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.


requests


upload_file


file.txt



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', 'http://example.com', files=files).prepare().body.decode('ascii'))
--c226ce13d09842658ffbd31e0563c6bd
Content-Disposition: form-data; name="upload_file"; filename="file.txt"


--c226ce13d09842658ffbd31e0563c6bd--



Note the filename="file.txt" parameter.


filename="file.txt"





Hi, How do I send multiple files sharing a same name? Like 'attachment' for example.
– William
Jun 6 at 9:29





@William: you can use a sequence of 2-value tuples too, which lets you re-use field names: files = [('attachment', open('attachment1.txt', 'rb')), ('attachment', open('attachment2.txt', 'rb'))]. Each tuple is a pair of key and value.
– Martijn Pieters
Jun 8 at 23:01




files = [('attachment', open('attachment1.txt', 'rb')), ('attachment', open('attachment2.txt', 'rb'))]





Just saved me a ton of time!!!
– PPJN
yesterday






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

CRM reporting Extension - SSRS instance is blank

Keycloak server returning user_not_found error when user is already imported with LDAP