python requests傳送multipart/form-data編碼
阿新 • • 發佈:2019-02-06
安裝requests_toolbelt
pip install requests-toolbelt
程式碼
傳送檔案中的資料
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(
fields={'field0': 'value', 'field1': 'value',
'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
)
r = requests.post('http://httpbin.org/post' , data=m,
headers={'Content-Type': m.content_type})
不需要檔案
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(fields={'field0': 'value', 'field1': 'value'})
r = requests.post('http://httpbin.org/post', data=m,
headers={'Content-Type' : m.content_type})