Created
June 22, 2020 06:03
-
-
Save Casxt/88e13b2956906e7d11f17d8cb8bfa22b to your computer and use it in GitHub Desktop.
python3 request multipart post example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import io | |
import json | |
from pprint import pprint | |
buffer = b'123123123' | |
params = [ | |
# 文本类型数据 | |
# RecoderId 是表单字段名 | |
# BreakIn 是字段内容 | |
# "text/plain" 是content-type | |
("RecoderId", (None, "BreakIn", 'text/plain')), | |
# 文件类型数据 | |
# RecorderImage 是表单字段名, | |
# image.jpg 告诉服务器的文件名, | |
# buffer是文件内容(字节), io.BytesIO(buffer) 将buffer包装成字节流, | |
# "image/jpeg" 是content-type | |
("RecorderImage", ("image.jpg", io.BytesIO(buffer), "image/jpeg")), | |
# 文件类型数据 | |
# 直接open本地文件也是可以的,open后得到字节流, | |
("RecorderImage", ("image.jpg", open(__file__, "rb"), "image/jpeg")) | |
] | |
r = requests.post("http://httpbin.org/post", files=params) | |
res = json.loads(r.text) | |
print(r.status_code) | |
pprint(res) | |
r.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment