文件的参数组装:
文件名',"open打开的文件(rb模式打开)",'文件的类型说明')
关于不同的请求参数类型,使用requests的处理:
1、文件上传(Content-Type: multipart/form-data;),使用files传递
文件参数)
2、表单参数(Content-Type: application/x-www-form-urlencoded;),使用data传递
表单参数)
3、json参数(Content-Type: application/json),使用json传递
参数)
4、查询字符串(拼接在url地址后面的),params传递
查询字符串参数)
"""
import requests
# 第一步:准备请求接口所需的数据
# 1、接口地址
url = "http://127.0.0.1:5000/upload"
# 2、请求参数
params = {
"nickname": "木森",
"age": 18,
"sex": "男"
}
# 文件的参数组装
file = {
"pic": ("kejian.ppt", open(r"C:\课件\课件模板.ppt", "rb"), "text/txt")
}
# 使用多个参数上传多个文件的时候,参数的组装形式
# file = {
# "pic": ("kejian.ppt", open(r"C:\课件\课件模板.ppt", "rb"), "text/txt"),
# "pic2": ("lmb.png", open("lmb.png", "rb"), "text/txt")
# }
# 同一个参数上传多个文件的时候,参数的组装形式
# files = [
# ("pic", ("kejian.ppt", open(r"C:\课件\课件模板.ppt", "rb"), "text/txt")),
# ("pic", ("lmb.png", open("lmb.png", "rb"), "text/txt")),
# ]
# 第二步发送请求 params data json,files
# 文件上传的接口,上传的文件需要使用files来进行传递
res = requests.post(url=url, data=params, files=file)
# 第三步:获取返回结果
print(res.json())