最近测试的接口是上传文件的接口,上传单个文件,我主要使用了2种方法~
接口例如:
URL: http://www.baidu.com/***
method:post
参数:
{ "salary": 19, "file": {} }
1、使用Python的requests上传表单数据和文件
data={"salary":salary}
files={'file': open(Con.file_path, 'rb')}
headers= {
"Authorization": "bearer " + self.token,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'
}
re = requests.post(URL, data=data, files=files,headers=headers)
2、使用python中使用multipart/form-data请求上传文件
m = MultipartEncoder(
fields={'filename': 'test01',
'version': '10001,
'file': ('test01.xlsx', open('C:\Users\Desktop\ma\test01.xlsx', 'rb'),
'application/octet-stream')})
headers = {
"Authorization": "bearer " + self.token,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
"Content-Type": m.content_type
}
re = requests.post(URL, data=m, headers=headers).json()