網站首頁 編程語言 正文
〇、前言
文件上傳/下載接口與普通接口類似,但是有細微的區別。
如果需要發送文件到服務器,例如:上傳文檔、圖片、視頻等,就需要發送二進制數據,上傳文件一般使用的都是 Content-Type: multipart/form-data 數據類型,可以發送文件,也可以發送相關的消息體數據。
反之,文件下載就是將二進制格式的響應內容存儲到本地,并根據需要下載的文件的格式來寫文件名,例如:F:/合同文件.pdf。
一、文件上傳接口
1. 接口文檔
Request URL: /createbyfile
Request Method: POST
Content-Type: multipart/form-data
名稱 | 類型 | 是否必須 | 描述 |
---|---|---|---|
file | File | 是 | 文檔文件 |
title | String | 是 | 文檔名稱 |
fileType | String | 是 | 文件類型:doc, docx, txt, pdf, png, gif, jpg, jpeg, tiff, html, rtf, xls, txt |
2. 代碼實現
(1)實現步驟:
構造文件數據,通過open函數以二進制方式打開文件
文件上傳接口參數與普通post請求一樣,需要寫成Key和Value模式,Key為參數名稱file(也是組件的name屬性),Value為一個元組(與普通接口不同的地方)
"file": ( "", # 元組第一個值為文件名稱,沒有則取None open(r"F:\pdf_file.pdf", "rb"), # 若第一個值非None,則取文件open打開的二進制流,否則直接寫文件路徑,如"F:\pdf_file.pdf" "pdf" # 文件類型 )
"file": ( None, "F:\pdf_file.pdf" )
構造其他數據
{ "title": "接口發起的文檔", "fileType": "pdf" }
發送請求,將文件數據以 files 參數傳入,其他消息體數據通過 data 、json 、 headers 、 cookies 等傳入
req = { "url": "127.0.0.1/v2/document/createbyfile", "method": "POST", "headers": {}, "files": {"file": ("", open(r"F:\pdf_file.pdf", "rb"), "pdf")}, "data": { "title": "接口發起的文檔", "fileType": "pdf" } }
(2)完整代碼
base_api.py
import requests class BaseApi: ? ? @staticmethod ? ? def requests_http(req): ? ? ? ? # ** 解包 ? ? ? ? result = requests.request(**req) ? ? ? ? return result
api/createbyfile.py
# -*- coding:utf-8 -*- # 作者:IT小學生蔡坨坨 # 時間:2022/3/12 21:04 # 功能:根據文件類型創建合同文檔 from base_api import BaseApi class Createbyfile: ? ? def createbyfile(self): ? ? ? ? req = { ? ? ? ? ? ? "url": "127.0.0.1/createbyfile", ? ? ? ? ? ? "method": "POST", ? ? ? ? ? ? "headers": {}, ? ? ? ? ? ? "files": {"file": ("", open(r"F:\pdf_file.pdf", "rb"), "pdf")}, ? ? ? ? ? ? "data": { ? ? ? ? ? ? ? ? "title": "接口發起的文檔", ? ? ? ? ? ? ? ? "fileType": "pdf" ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? res = BaseApi().requests_http(req) ? ? ? ? assert res.status_code == 200 ? ? ? ? res_json = res.json() ? ? ? ? return res_json["result"]["documentId"] if __name__ == '__main__': ? ? Createbyfile().createbyfile()
二、文件下載接口
1. 接口文檔
Request URL:/download
Request Method:GET
名稱 | 類型 | 是否必須 | 描述 |
---|---|---|---|
contractId | Long | ID | ID |
downloadItems | String[] | 否 | 下載可選項,NORMAL(正文),ATTACHMENT(附件) |
needCompressForOneFile | Boolean | 是,默認單文件也壓縮 | 當下載的文件僅一份時,是否壓縮 |
2. 代碼實現
# -*- coding:utf-8 -*- # 作者:IT小學生蔡坨坨 # 時間:2022/4/5 2:56 # 功能:下載合同 from base_api import BaseApi class Download: ? ? def download(self): ? ? ? ? req = { ? ? ? ? ? ? "url": "127.0.0.1/download", ? ? ? ? ? ? "method": "GET", ? ? ? ? ? ? "headers": {}, ? ? ? ? ? ? "params": { ? ? ? ? ? ? ? ? "contractId": 2947403075747869536, ? ? ? ? ? ? ? ? "downloadItems": ["NORMAL"], ? ? ? ? ? ? ? ? "needCompressForOneFile": False ? ? ? ? ? ? }, ? ? ? ? } ? ? ? ? res = BaseApi().requests_http(req).content # 注意“.content"獲取返回內容 ? ? ? ? # with open("F:/response.zip", "wb") as f: ? ? ? ? with open("F:/response.pdf", "wb") as f: ? ? ? ? ? ? f.write(res) ? ? ? ? return res if __name__ == '__main__': ? ? Download().download()
總結
原文鏈接:https://www.cnblogs.com/caituotuo/p/16101337.html
相關推薦
- 2023-01-17 如何使用python中的networkx來生成一個圖_python
- 2022-06-16 c語言單詞搜索的實現_C 語言
- 2022-09-17 C++中stack的pop()函數返回值解析_C 語言
- 2022-09-17 Python?pandas?重命名索引和列名稱的實現_python
- 2022-01-08 iframe 監聽滾動事件并滾動到指定位置
- 2022-12-25 python實現超時退出的三種方式總結_python
- 2021-12-22 Linux一次性計劃任務at命令使用詳解_Linux
- 2023-02-05 詳解Pytorch中Dataset的使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支