詳解flask中如何獲取不請求方式的參數(shù)
前言
基于現(xiàn)在主流前后端交互的api,那么就來看看如何使用flask實(shí)現(xiàn)參數(shù)接受的。這里是我常用的方式,關(guān)于更多使用方式,請點(diǎn)擊這里去文檔查看更多
一、get請求下
1.1、路徑中帶有參數(shù)
1.1.1、postman示例
1.1.2、 flask代碼實(shí)現(xiàn)
@blog_base_blueprint.route('/path_posts/<int:resource_id>', methods=['GET']) def get_post_path(resource_id): resource_lists: List[models.Resource] = (models.Resource.query .filter(models.Resource.id == resource_id) .all()) return jsonify({ 'code': 0, 'msg': 'success', 'data': { "resource_list": [resource.to_format() for resource in resource_lists] } })
1.1.3、注意事項(xiàng)
在注冊路由的時候一定要寫上參數(shù)和參數(shù)的類型定義方法的時候一定寫上參數(shù)名,這樣才能直接使用
1.2、在url后邊帶有參數(shù)
當(dāng)參數(shù)以 params 形式傳遞,也就是參數(shù)以?a=x&b=y 的形式顯示在 url 中訪問的形式
1.2.1、postman示例
1.2.2、 flask代碼實(shí)現(xiàn)
通過request.args.get(‘key’)
和 request.values.get(‘key’)
來接收參數(shù)
@blog_base_blueprint.route('/posts', methods=['GET']) def get_posts(): current_app.logger.info('獲取列表') resource_id = request.args.get('resource_id', 2) resource_lists: List[models.Resource] = (models.Resource.query .filter(models.Resource.id == resource_id) .all()) return jsonify({ 'code': 0, 'msg': 'success', 'data': { "resource_list": [resource.to_format() for resource in resource_lists] } })
二、post請求下
前后端API交互時,常見的還是post的請求方式,且常用的有form-data 或x-www-form-urlendoded形式,還有為json數(shù)據(jù)的body體
2.1、form-data格式
2.1.1、postman示例
2.1.2、flask代碼實(shí)現(xiàn)
使用request.form
獲取
@blog_base_blueprint.route('/add_post', methods=['POST', 'GET']) def create_or_update_resource(): title = request.form.get('title') json_data = request.form return jsonify({ 'json_data' : json_data, 'title' : title, })
2.2、www-form-urlendoded格式
2.2.1、postman示例
2.2.2、flask代碼實(shí)現(xiàn)
使用request.form
接收參數(shù)
#測試www-form-urlendoded格式 @blog_base_blueprint.route('/test1', methods=['POST']) def test1(): title = request.form.get('title2') json_data = request.form id = request.values.get('id') return jsonify({ 'json_data' : json_data, 'title' : title, 'id' : id, })
2.3、json傳參格式
2.3.1、postman示例
2.3.2、flask代碼實(shí)現(xiàn)
使用request.get_json()
和 request.get_data()
實(shí)現(xiàn),使用方法參考點(diǎn)擊查看使用文檔
#測試json格式 @blog_base_blueprint.route('/test1_json', methods=['POST']) def test1_json(): json_data1 = request.get_data(as_text=True) print(json_data1) json_data2 = request.get_json() title = request.json.get('title') return jsonify({ 'json_data1' : json_data1, 'json_data2' : json_data2, 'title' : title, })
2.3.3、注意事項(xiàng)
根據(jù)結(jié)果可以看到:
- get_data()方法,獲取到的是未經(jīng)處理的原始數(shù)據(jù),如果數(shù)據(jù)格式是json的,則取得是json字符串,排序和請求參數(shù)一致
- get_json()方法,將請求參數(shù)做了處理,得到字典格式,因此排序會打亂,依據(jù)字典排序規(guī)則。
2.4、headers傳參格式
很多時候,為了安全性,接口交互時會傳遞token,這時候是使用request.headers
來接收headers里的token的
2.4.1、postman示例
2.4.2、flask代碼實(shí)現(xiàn)
使用request.headers
實(shí)現(xiàn)
@blog_base_blueprint.route('/add_post', methods=['POST', 'GET']) def create_or_update_resource(): token = request.headers.get('Authorization', '') title = request.form.get('title') # json_data = request.form return jsonify({ 'token' : token, })
以上就是詳解flask中如何獲取不請求方式的參數(shù)的詳細(xì)內(nèi)容,更多關(guān)于flask獲取參數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)現(xiàn)對特定列表進(jìn)行從小到大排序操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)對特定列表進(jìn)行從小到大排序操作,涉及Python文件讀取、計算、正則匹配、排序等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02Python3 itchat實(shí)現(xiàn)微信定時發(fā)送群消息的實(shí)例代碼
使用微信,定時往指定的微信群里發(fā)送指定信息。接下來通過本文給大家分享Python3 itchat實(shí)現(xiàn)微信定時發(fā)送群消息的實(shí)例代碼,需要的朋友可以參考下2019-07-07Python跨文件調(diào)用函數(shù)的五種實(shí)用方法
在開發(fā)Python項(xiàng)目時,90%的開發(fā)者都會遇到這樣的困境:代碼越寫越長,功能越來越亂,最后變成難以維護(hù)的"意大利面條式代碼",本文將手把手教你通過模塊化編程,讓代碼結(jié)構(gòu)清晰、可維護(hù)性強(qiáng),并深入解析5種跨文件調(diào)用函數(shù)的實(shí)用方法,需要的朋友可以參考下2025-05-05Python實(shí)現(xiàn)LSTM學(xué)習(xí)的三維軌跡
這篇文章主要為大家詳細(xì)介紹了如何使用LSTM來學(xué)習(xí)和預(yù)測三維軌跡,并提供詳細(xì)的Python實(shí)現(xiàn)示例,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-12-12基于Python實(shí)現(xiàn)簡單的學(xué)生點(diǎn)名系統(tǒng)
現(xiàn)在的學(xué)生大部分都很積極,會主動舉手回答問題。但是,也會遇到一些不好的情況,比如年級越高主動舉手的人越少,所以本文做了一個隨機(jī)的學(xué)生點(diǎn)名系統(tǒng)可以幫老師解決這些問題2022-09-09