Reputation: 65
I want to upload an image using requests module (Python 3). Sadly, the server answers my request with an error, saying that I should only upload files of type jpg, png or gif. I guess I should fill every field of the form, but I want to figure out how despite all my tests.
Here is the HTML form :
<form class="upload" enctype="multipart/form-data" action="?action=upload" method="post">
<h3>Envoyez votre image !</h3>
<input name="MAX_FILE_SIZE" value="15360000" type="hidden" />
<input name="img" size="30" type="file" />
<input value="Envoyer" type="submit" />
</form>
I use this Python code :
with open(image, 'rb') as img:
toup = {'img': (os.path.basename(image), img)}
res= requests.post('http://url/?action=upload', files = toup)
How can I fill the MAX_FILE_SIZE
field and specify the type of the uploaded file?
Upvotes: 1
Views: 2028
Reputation: 902
You could set the mime type of the file:
toup = {'img': (os.path.basename(image), img, 'image/jpeg')}
To get exact MIME of the file, you can use mimetypes library.
Upvotes: 1
Reputation: 3784
MAX_FILE_SIZE seems to be a normal HTTP POST data:
with open(image, 'rb') as img:
toup = {'img': (os.path.basename(image), img)}
res= requests.post('http://url/?action=upload', files = toup, data={'MAX_FILE_SIZE': '15360000'})
Upvotes: 0