requests上传文件

from pprint import pprint
import requests

host = 'http://127.0.0.1'
user = {
    'username': 'showdoc',
    'password':'123456'
}

s = requests.Session() # => 会话对象

# 登录
login_url = parse.urljoin(host, '/showdoc/server/index.php?s=/api/user/login')
lr = s.post(login_url, data=user)
pprint(lr.json())

# 上传图片
upload_url = parse.urljoin(host,'http://127.0.0.1/showdoc/server/index.php?s=/api/page/uploadImg')
# 构造图片数据,这里必须要填上图片相关参数
file = {
    'editormd-image-file': open(r'D:\data\1.png', 'rb'),   # => 用name指定文件
    'Content-Disposition': 'form-data', 
    'Content-Type': 'image/png', 
    'filename':'1.png'
    }
ur = s.post(upload_url, files=file)  # => 注意这里,参数名是 files
pprint(ur.json())
posted @ 2021-11-10 21:48  总得前行  阅读(116)  评论(0编辑  收藏  举报