一、環(huán)境描述
python:3.8.0
flask: 2.3.2
postman:9.12.2
Flask delete&put請求傳送門:FLASK DELETE&PUT
二、初始化flask 程序
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)
-
0.0.0.0
: 由于我使用的是 虛擬機(jī),所以指定同一局域網(wǎng)中可反問 -
debug
:開啟debug模式,修改程序后會自動部署,無需重啟程序
三、get請求
3.1 代碼
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
# get 獲取請求參數(shù)
@app.route('/user', methods=['GET'])
def get_user_info():
# user_name = request.args.get('user_name')
user_name = request.values.get("user_name")
# 將數(shù)據(jù)再次打包為 JSON 并傳回
res = {"user_name": user_name}
return res
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)
3.2 分析
-
請求舉例:http://ip:5000/user?user_name=neil
-
限定請求只響應(yīng)路徑為
/user
時的get
請求@app.route('/user', methods=['GET']) def method(): ...
- 使用
/user
限制請求響應(yīng)的路徑 - 使用
methods
進(jìn)行請求方式限定
- 使用
-
獲取參數(shù)的方式
# 獲取get ?后參數(shù)的方法 request.args.get('user_name') request.values.get('user_name')
3.3 驗證
-
postman 請求參數(shù)
-
postman header 如下(使用默認(rèn))
3.4 請求結(jié)果
四、post請求
4.1 代碼
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def hello_world(): # put application's code here
return 'Hello World!'
# post 獲取請求參數(shù)
@app.route('/user', methods=['POST'])
def post_user_info():
# postman 使用默認(rèn)請求頭application/json
user_info = request.get_json()
res = {"user_name": user_info.get("user_name")}
# postman 使用application/x-www-form-urlencoded請求
# 注:此測試得也可解析multipart/form-data請求
# user_name = request.values.get('user_name')
# res = {"user_name": user_name}
# postman 使用multipart/form-data請求
# 注:此測試得也可解析application/x-www-form-urlencoded請求
# user_name = request.form.get('user_name')
# user_name = request.form['user_name']
# res = {"user_name": user_name}
# postman 使用上述三種都可以收到內(nèi)容,但是返回值為bytes類型
# user_info = request.get_data()
# res = {"user_name": user_info.decode("utf-8")}
return res
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)
4.2 分析
-
請求舉例:http://ip:5000/user
-
限定請求只響應(yīng)路徑為
/user
時的post
請求@app.route('/user', methods=['POST']) def method(): ...
- 使用
/user
限制請求響應(yīng)的路徑 - 使用
methods
進(jìn)行請求方式限定
- 使用
-
獲取參數(shù)的方式有四種
-
request.get_json()
:可解析 請求頭application/json的參數(shù) -
request.values.get('user_name')
: 可解析請求頭為application/x-www-form-urlencoded & multipart/form-data的參數(shù) -
request.form.get('user_name')&request.form['user_name']
: 可解析請求頭為multipart/form-data &application/x-www-form-urlencoded的參數(shù) -
request.get\_data()
:怎樣的請求都可以拿到參數(shù),但是默認(rèn)初始解析返回值是bytes
的對象
-
4.3 驗證
4.3.1 postman 請求頭application/json參數(shù)
-
設(shè)置參數(shù)
-
header內(nèi)容
-
請求結(jié)果
4.3.2 postman 請求頭application/x-www-form-urlencoded參數(shù)
-
設(shè)置參數(shù)
-
header內(nèi)容
-
請求結(jié)果
4.3.3 postman 請求頭multipart/form-data參數(shù)
- 設(shè)置參數(shù)
- header內(nèi)容
- 請求結(jié)果
4.3.4 request.get_data()解析參數(shù)
- 請求multipart/form-data請求頭內(nèi)容
- 請求 application/x-www-form-urlencoded請求頭內(nèi)容
- 請求application/json請求頭內(nèi)容
五、總結(jié)
-
get
請求GET
把參數(shù)包含在URL
中,訪問時會在地址欄直接顯示參數(shù)不安全,且參數(shù)大小比較小 -
post
請求文章來源:http://www.zghlxwxcb.cn/news/detail-608633.html參數(shù)通過
request body
傳遞,請求頭的情況下需要不同的解析方式進(jìn)行相關(guān)處理文章來源地址http://www.zghlxwxcb.cn/news/detail-608633.html
六、遺留問題
- 為什么application/x-www-form-urlencoded & multipart/form-data 請求頭可以相互解析相關(guān)參數(shù)?
- x-www-form-urlencoded,表單默認(rèn)的 Content-type 類型,支持 ASCII-text 文本內(nèi)容
- multipart/form-data,允許提交表單包含: files,non-ASCII-text,Binary 類型數(shù)據(jù)
- 參考:https://learning.postman.com/docs/sending-requests/requests/
到了這里,關(guān)于Flask get &post請求的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!