Send multipart / form-data with python requests

I often forget it, so make a note.

import requests

def send():
	image = open('/path/to/file', 'rb')
	files = {'param_name': ('filename.jpg', image, 'image/jpeg')}
	data = {'another_key': 'another_value'}
	r = requests.post(URL, files=files, data=data)
	print r.text

You can specify the file name and content-type by setting the value of the contents of files to tuple.

Reference: https://github.com/kennethreitz/requests/issues/1495#issuecomment-21581939

Recommended Posts

Send multipart / form-data with python requests
Retry with python requests
Send email with Python
Send Japanese email with Python3
Send using Python with Gmail
[Automation] Send Outlook email with Python
[Python] POST wav files with requests [POST]
Get the weather with Python requests
Get the weather with Python requests 2
[Python] Send an email with outlook
Send image with python, save with php
Send email via gmail with Python 3.4.3.
HTML email with image to send with python
Send an email with Amazon SES + Python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Integrate with Python
AES256 with python
Tested with Python
[Python] Send email
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Send a message to LINE with Python (LINE Notify)
Send an email to Spushi's address with python
Sample to send slack notification with python lambda
Send HTTP with Basic authentication header in Python
Send an email with Excel attached in Python
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learn Python with ChemTHEATER
Run prepDE.py with python3
Collecting tweets with Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python