flask-study-004
本篇博客记录学习和实践flask的request全局对象的属性和方法
1. request的属性
属性名 | 解释 | 示例 |
---|---|---|
form | 从post或者put请求解析的multidict(一键多值字典) | |
args | multidict,要操作的url中提交的参数,可以使用args属性。 | ?key=value。search = request.args.get('key') |
values | combinemultidict,内容是forms和args | |
cookie | 请求的cookie,类型是dict | |
stream | ||
headers | 请求头,字典类型 | |
data | 请求数据 | |
files | ||
environ | ||
method | 请求的方法。 | 比如post,get |
path | 请求路径 |
2. request的常用属性和方法
2.1 request.method
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/', methods=['GET', 'POST'])
def index():
r_method = request.method
return f"hello,{r_method}"
if __name__ == "__main__":
app.run()
终端执行:curl -H "Content-Type: application/json" -X GET http://127.0.0.1:5000/
结果显示:hello,GET
终端执行:curl -H "Content-Type: application/json" -X POST http://127.0.0.1:5000/
结果显示:hello,POST
2.2 request.args
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/', methods=['GET', 'POST'])
def index():
word = request.args.get('name', 'liwl')
return f"hello,{word}"
if __name__ == "__main__":
app.run()
终端执行:curl -H "Content-Type: application/json" -X GET http://127.0.0.1:5000/?name=liwanliang
结果显示:hello,liwaniang
终端执行:curl -H "Content-Type: application/json" -X GET http://127.0.0.1:5000/
结果显示:hello,liwl
2.3 request.form
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/', methods=['POST'])
def index():
username = request.form.get('username')
password = request.form.get('password')
return f"hello,{username}.your password is {password}"
if __name__ == "__main__":
app.run()
终端执行:curl -H "Content-Type: multipart/form-data" -X POST --form 'username=liwl' --form 'password=liwanliang' http://127.0.0.1:5000/
结果显示:hello,liwl.your password is liwanliang
2.4 request.cookies
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/')
def index():
username = request.cookies.get('username')
print(request.cookies)
return f"hello, {username} in cookie"
if __name__ == "__main__":
app.run()
终端执行:curl -X GET http:/127.0.0.1:5000 --cookie "username=liwl"
结果显示:hello, liwl in cookie
2.5 request.headers
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/', methods=['POST', 'GET'])
def index():
user_agent = request.headers.get('User-Agent')
return f'your user agent is {user_agent}'
if __name__ == "__main__":
app.run()
终端执行:curl -X POST http://127.0.0.1:5000
结果显示:your user agent is curl/7.64.0
浏览器访问:http://127.0.0.1:5000 ,结果显示:
your user agent is Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
2.6 request.get_json()
request.get_json方法用于获取请求中的json数据,便于将其转换为python字典或者其他数据类型
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/', methods=['POST'])
def index():
data = request.get_json()
username = data.get('username')
password = data.get('password')
return f"username: {username}, password: {password}"
if __name__ == "__main__":
app.run()
终端执行:curl -H "Content-Type: application/json" -X POST -d '{"username":"liwl","password":"liwanliang"}' http://127.0.0.1:5000/
结果显示:username: liwl, password: liwanliang
2.6 request.data
request.data属性,用于获取请求中的非表单数据。一般非表单数据包括:
json数据(api接口),文件上传数据(图片,视频,文档),原始文本数据(日志,配置文件),二进制数据(图片,音视频),xml数据等
2.7 request.files
from flask import Flask, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/', methods=['POST'])
def index():
file = request.files['file']
file.save("/tmp/"+file.filename)
return 'success'
if __name__ == "__main__":
app.run()
终端执行:curl -F "file=@liwl.txt" http://127.0.0.1:5000/
结果显示:success
查看:ls -lrt /tmp/liwl.txt