〇、前言
文件上传/下载接口与普通接口类似,但是有细微的区别。
如果需要发送文件到服务器,例如:上传文档、图片、视频等,就需要发送二进制数据,上传文件一般使用的都是 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()
总结
到此这篇关于Python接口自动化之文件上传/下载接口的文章就介绍到这了,更多相关Python文件上传/下载接口内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
查看更多关于Python接口自动化之文件上传/下载接口详解的详细内容...