python调用接口上传文件_python上传文件接口
文件的參數(shù)組裝:
('文件名',"open打開的文件(rb模式打開)",'文件的類型說明')
關(guān)于不同的請求參數(shù)類型,使用requests的處理:
1、文件上傳(Content-Type: multipart/form-data;),使用files傳遞
requests.post(url='xxx',files=文件參數(shù))
2、表單參數(shù)(Content-Type: application/x-www-form-urlencoded;),使用data傳遞
requests.post(url='xxx',data=表單參數(shù))
3、json參數(shù)(Content-Type: application/json),使用json傳遞
requests.post(url='xxx',json=json參數(shù))
4、查詢字符串(拼接在url地址后面的),params傳遞
requests.post(url='xxx',params=查詢字符串參數(shù))
"""
import requests
# 第一步:準(zhǔn)備請求接口所需的數(shù)據(jù)
# 1、接口地址
url = "http://127.0.0.1:5000/upload"
# 2、請求參數(shù)
params = {
"nickname": "木森",
"age": 18,
"sex": "男"
}
# 文件的參數(shù)組裝
file = {
"pic": ("kejian.ppt", open(r"C:\課件\課件模板.ppt", "rb"), "text/txt")
}
# 使用多個參數(shù)上傳多個文件的時候,參數(shù)的組裝形式
# file = {
# "pic": ("kejian.ppt", open(r"C:\課件\課件模板.ppt", "rb"), "text/txt"),
# "pic2": ("lmb.png", open("lmb.png", "rb"), "text/txt")
# }
# 同一個參數(shù)上傳多個文件的時候,參數(shù)的組裝形式
# files = [
# ("pic", ("kejian.ppt", open(r"C:\課件\課件模板.ppt", "rb"), "text/txt")),
# ("pic", ("lmb.png", open("lmb.png", "rb"), "text/txt")),
# ]
# 第二步發(fā)送請求 params data json,files
# 文件上傳的接口,上傳的文件需要使用files來進(jìn)行傳遞
res = requests.post(url=url, data=params, files=file)
# 第三步:獲取返回結(jié)果
print(res.json())
標(biāo)簽:python,open,接口,url,json,參數(shù),rb,requests,上傳
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的python调用接口上传文件_python上传文件接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab 直方图_MATLAB作图实
- 下一篇: 人们通常先在线性表尾部临时添加一个_数据