'Upload files with python request with multipart-form data
I need to send images with request like the browser does. I receive a correct reply [200] but files are not received since when I do it with the browser I receive back html with imgs tag. This website does not make use of complex system and uses only phpsessid cookie so I think the is how request is encoded
<form action="dettaglio_img.php?id_art=4129" enctype="multipart/form-data" method="post">
<input type="file" name="art_img[]" multiple="multiple" accept="image/*">
<input type="submit" name="salva_img" value="Salva immagine">
</form>
payload
------WebKitFormBoundaryEV9uoGTWSomWcynk
Content-Disposition: form-data; name="art_img[]"; filename=""
Content-Type: application/octet-stream
------WebKitFormBoundaryEV9uoGTWSomWcynk
Content-Disposition: form-data; name="salva_img"
Salva immagine
------WebKitFormBoundaryEV9uoGTWSomWcynk--
# python
with requests.session() as session:
cookie = login(session, h)
abs_path = (os.path.join(path, file) for file in os.listdir(path))
files = (("", open(file, 'rb'), Image.open(file).get_format_mimetype()) for file in abs_path)
images = [('art_img[]', file) for file in files]
response = session.post(
"https://localhost/titanium/articoli/dettaglio_img.php?id_art=4129",
files=images,
data={'salva_img': "Salva immagine"}
)
print(response.status_code)
print(response.text)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
