This is the 26th day of my participation in the August Text Challenge.More challenges in August

background

Upload and download interface is an infrequent but essential user behavior interface in practical application. For example, in the front end of the APP, you need to upload the head picture and send pictures to the moments of friends. The Web background requires uploading files and downloading templates. Does that sound familiar? But sometimes you need to use interface testing tools or code implementation, there is always no small difficulty, will inevitably go astray! Because implementers do not have a clear understanding of such interfaces, and because implementers do not understand how to prepare for interface requests, Python takes you through the process.Copy the code
Practice 1: [Upload an XLSX File]

Either looking at the interface documentation or using the packet capture tool (F12) to get the necessary information about the interface is not as clear as actually doing it:

The request header of the interface for uploading files is in form-data format. The parameter name is file and the value is the file path. The miME-type of the uploaded file is < Files Upload TAB >.

Python implementation

The interface request parameters are constructed:

import requests 
from urllib3.filepost import encode_multipart_formdata 
url="Http://192.168.2.58:9100/tool/generatePlazaPlan? tag=711f1a4104e94295bebd031cadded28b" 
# headers={"Content-Type":"multipart/form-data"} 
data={"file": ("Level 4 CET - 4. XLSX".open(r"C:\WXWork\1688850343061144\Cache\File\2021-03\CET-4.xlsx"."rb"),"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")} 
Send the interface request and print the result
res=requests.post(url,files=data).json() 
print(res)
Copy the code

Do not set the request header content-type in requests because the request header was not found or not known. The request was rejected because no multipart boundary was found; Then there is data: file, which will set different values according to different file types.

Practice 2: [File Uploading an image]

Interface analysis: this is image, its type is image/ JPEG; Then do you do the same as uploading an XLSX file?

Code implementation

import requests 
from urllib3.filepost import encode_multipart_formdata 
url="http://192.168.2.128:9100/manager/api/v1/challenge/modifyQRCode" 
file_data={"challengeId":26."file": ("iamge.jpg".open(r"D:\javaworkspace\scripts\practices\iamge.jpg"."rb"),"image/jpeg")} 
headers={"Authorization":"eyJ0eXAiOiJKV1QiLCJhbGciO"} 
Failed to send the interface request
res1=requests.post(url, files=file_data, headers=headers).json() 
print(res1)
Copy the code

F12: formdata has an extra parameter, and it has a token request, so it needs to be converted to encode_multipart_formDATA and placed in the request header, and the request parameter is not files but data

import requests 
from urllib3.filepost import encode_multipart_formdata 
url="http://192.168.2.128:9100/manager/api/v1/challenge/modifyQRCode" 
If there are other request parameters, they need to be included
file_data={"challengeId":26."file": ("iamge.jpg".open(r"D:\javaworkspace\scripts\practices\iamge.jpg"."rb"),"image/jpeg")} 
headers={"Authorization":"eyJ0eXAiOiJKV1QiLCJhbGciO"} 
encode_data = encode_multipart_formdata(data) 
headers["Content-Type"]=encode_data[1] 
res1=requests.post(url, files=file_data, headers=headers).json() 
print(res1)
Copy the code

This time the output is correct

Another way of writing # file_data
with open(r"D:\javaworkspace\scripts\practices\iamge.jpg"."rb") as pf: file_data={"challengeId":26."file": ("iamge.jpg",pf.read())}
Copy the code

conclusion

The request mode of requests library to complete the file upload interface depends on the format of the file upload interface and the actual application scenario.

But anyway, it is necessary to do a good job in the corresponding time knowledge reserve.