Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the automatic file upload / download interface of Python interface

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to realize the file upload / download interface automation of the Python interface. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

0. Preface

The file upload / download interface is similar to the normal interface, but with slight differences.

If you need to send files to the server, such as uploading documents, pictures, videos, etc., you need to send binary data. Files are usually uploaded using Content-Type: multipart/form-data data type. You can send files or relevant message body data.

File download, on the other hand, stores the response content in binary format locally and writes the file name according to the format of the file you need to download, for example: Faffle / contract file .pdf.

1. File upload interface 1. Interface document

Request URL: / createbyfile

Request Method: POST

Content-Type: multipart/form-data

Whether the name type must describe whether fileFile is a document file titleString is a document name fileTypeString is a file type: doc, docx, txt, pdf, png, gif, jpg, jpeg, tiff, html, rtf, xls, txt2. Code implementation

(1) implementation steps:

Construct the file data and open the file in a binary way through the open function

The parameters of the file upload interface, like ordinary post requests, need to be written in Key and Value modes. Key is the parameter name file (which is also the name attribute of the component), and Value is a tuple (different from the normal API).

"file": (",", the first value of # tuple is the file name. If not, take None open (r "F:\ pdf_file.pdf", "rb"). # if the first value is not None, take the binary stream opened by the file open, otherwise write the file path directly. For example, "F:\ pdf_file.pdf"pdf" # file type) "file": (None, "F:\ pdf_file.pdf")

Construct other data

{"title": "documentation initiated by API", "fileType": "pdf"}

Send the request, pass in the file data as files parameter, and other message body data through data, json, headers, cookies, etc.

Req = {"url": "127.0.0.1/v2/document/createbyfile", "method": "POST", "headers": {}, "files": {"file": (", open (r" F:\ pdf_file.pdf "," rb ")," pdf ")} "data": {"title": "documentation initiated by API", "fileType": "pdf"}}

(2) complete code

Base_api.py

Import requestsclass BaseApi: @ staticmethod def requests_http (req): # * * unpack result = requests.request (* * req) return result

Api/createbyfile.py

#-*-coding:utf-8-*-# author: IT pupil Cai Tuotuo # time: 2022-3-12 21VOVA function: create contract documents according to file type from base_api import BaseApiclass 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 ":" documentation initiated by the API " "fileType": "pdf"} res = BaseApi (). Requests_http (req) assert res.status_code = = 200res_json = res.json () return res_json ["result"] ["documentId"] if _ _ name__ = ='_ main__': Createbyfile (). Createbyfile () 2, file download interface 1. Interface document

Request URL:/download

Request Method:GET

Whether the name type must describe whether contractIdLongIDIDdownloadItemsString [] is optional to download, NORMAL (body), ATTACHMENT (attachment) needCompressForOneFileBoolean is, the default list file is also compressed when there is only one downloaded file, whether to compress 2. Code implementation #-*-coding:utf-8-*-# author: IT pupil Cai Tuotuo # time: 2022-4-5 2def download 5pm function: download contract from base_api import BaseApiclass 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 # Note ".content" get the returned content # with open ("F:/response.zip") "wb") as f: with open ("F:/response.pdf", "wb") as f: f.write (res) return resif _ name__ = ='_ main__': Download (). Download () these are all the contents of the article "how to implement the file upload / download interface for Python interface automation" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report