本篇博客記錄學習和實踐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 "[email protected]" http://127.0.0.1:5000/
結果顯示:success
查看:ls -lrt /tmp/liwl.txt