python代码- post请求图片上传

postman中 的一个请求接口,post请求

 请求特点:其他请求参数均正常,text类型,只有file字段是file类型

捕捉了一下,

 这样的post请求,用headers为 Content-Type 用Application/Json 或 Application/x-www-form-urlencoded,这两种常用 json或form,python post请求。均无法请求成功,返回500错误。

 请求 抓包,headers 中 的Content-Type:multipart/form-data 注释,只要上面那个

解决方法如上。 python 中代码:

url = '%s:18080/oss/file/upload' % url
files = {'file':('front.png',open('C:/Users/fang/front.png','rb'),'image/png')}
headers = {'Postman-Token':'fbb65c0f-ce30-4183-9ee3-616c535a9977'}
data = originmessage.youzanOSSUPLoad.copy() # data 是个dict data中字段比较多,一起upLoad中
re = requests.post(url,headers=headers,data=data,files=files)
print(re.text,re.json())

file图片文件,直接发送files参数。其他字段发送data参数即可

关键点:headers 中 content-type 一定要删除, 只发送 token 相关即可。

写这个请求的时候,查了资料,与正常请求方式麻烦点,记录下

原文地址:https://www.cnblogs.com/MTXue/p/13920078.html