LoginSignup
36
24

More than 5 years have passed since last update.

pythonのrequestsでmultipart/form-data送信

Last updated at Posted at 2015-12-03

よく忘れるのでメモ。

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

filesの中身の値をtupleにすることで、ファイル名とcontent-typeを指定できる。

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

36
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
24