使用python中的requests库调用接口上传文件

时间:2022-11-17 12:59:27
import requests
url = "https://192.168.4.103/iof/base/resource/addResourceBf"
payload={'resoId': 'reso_fenlei16568654548266IP134232246455',
'resoName': '1234444455652345',
'resoDesc': '1',
'resTag': '23',
'resoVersion': '123',
'rid': '16686545484141',
'modeType': '0',
'authMode': '1',
'encrypt': '0'}
files=[
('modeFile',('1w.csv',open('C:/Users/hanfe/Desktop/1w.csv','rb'),'text/csv'))
]
headers = {
'Cookie': '“Secure”; JSESSIONID=446D97EEB739E13C88FF4C61E23DBF9B'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files,verify=False)
print(response.text)


注意: 通过抓包显示请求头中包括 Content-Type:multipart/form-data,但是调用接口时不要写进去啊,否则会下面错误

Failed to parse multipart servlet request; nested exception is java.io.IOException:
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary
was found org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request;
nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found