文件的參數組裝:
('文件名',"open打開的文件(rb模式打開)",'文件的類型說明')
關於不同的請求參數類型,使用requests的處理:
1、文件上傳(Content-Type: multipart/form-data;),使用files傳遞
requests.post(url='xxx',files=文件參數)
2、表單參數(Content-Type: application/x-www-form-urlencoded;),使用data傳遞
requests.post(url='xxx',data=表單參數)
3、json參數(Content-Type: application/json),使用json傳遞
requests.post(url='xxx',json=json參數)
4、查詢字符串(拼接在url地址后面的),params傳遞
requests.post(url='xxx',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())