待整理,参考文档https://blog.csdn.net/zhubaojay/article/details/106915881?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-3&spm=1001.2101.3001.4242
区别在headers种content-type
form形式表单传参
验证3种上传file的方法:
这种会报错:data mast not be string
def handlePost(url,**kwargs):
data_json=kwargs.get('data')
if not isinstance(kwargs.get('data'),str):
data_json=json.dumps(kwargs.get('data'))
headers=kwargs.get('headers')
rep=requests.post(url,data=data_json.encode("utf-8"),headers=headers,params=kwargs.get('params'), files=kwargs.get('files'), timeout=(1000,1000),verify=False)
return rep
def handlePost(url, **kwargs):
data = kwargs.get('data')
if type(data).__name__ == 'dict':
data = json.dumps(kwargs.get('data'))
else:
data = kwargs.get('data')
headers = kwargs.get('headers')
# print("handlePost")
# print(url)
# print(data)
# print(kwargs.get('params'))
# print(kwargs.get('files'))
# print(headers)
# rep = requests.post(url, data=data_json.encode("utf-8"), headers=headers, params=kwargs.get('params'), files=kwargs.get('files'), timeout=(1000,1000),verify=False)
rep = requests.post(url, data=data, headers=headers, params=kwargs.get('params'), files=kwargs.get('files'), timeout=(1000, 1000), verify=False)
return rep
def handlePost(url,**kwargs):
data_json=kwargs.get('data')
if not isinstance(kwargs.get('data'),str):
# data_json=json.dumps(kwargs.get('data'))
data_json=kwargs.get('data')
headers=kwargs.get('headers')
# rep=requests.post(url,data=data_json.encode("utf-8"),headers=headers,params=kwargs.get('params'), files=kwargs.get('files'), timeout=(1000,1000),verify=False)
rep=requests.post(url,json=data_json,headers=headers,params=kwargs.get('params'), files=kwargs.get('files'), timeout=(1000,1000),verify=False)
return rep
总结,对于body只有1个参数_file:xxxobj,以下场景均可成功(虽然不知道为什么,先用起来,以后再做研究,如果有知道的大神,烦请教教我,谢谢):
files = [("_file", open(image_path, 'rb'))]