日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Flask】Request和RequestParser类

發布時間:2025/3/21 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flask】Request和RequestParser类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、RequestParser類

Flask-RESTful 提供了RequestParser類,用來幫助我們檢驗和轉換請求數據。

使用步驟:
1.創建RequestParser對象
2. 向RequestParser對象中添加需要檢驗或轉換的參數聲明
3. 使用parse_args()方法啟動檢驗處理
4. 檢驗之后從檢驗結果中獲取參數時可按照字典操作或對象屬性操作
代碼塊:

2、參數說明

1、required:描述請求是否一定要攜帶對應參數,默認值為False

  • True 強制要求攜帶;若未攜帶,則校驗失敗,向客戶端返回錯誤信息,狀態碼400
    rq.add_argument('a',type=int,required=True,help='參數a錯誤')
  • False 不強制要求攜帶;若不強制攜帶,在客戶端請求未攜帶參數時,取出值為None
    rq.add_argument('a',help='參數a錯誤')

2、help:參數檢驗錯誤時返回的錯誤描述信息
rq.add_argument('a',type=int,required=True,help='參數a錯誤')
3、action:描述對于請求參數中出現多個同名參數時的處理方式

  • action=‘store’ 保留出現的第一個, 默認
  • action=‘append’ 以列表追加保存所有同名參數的值
    rq.add_argument('b',type=str,required=True,help='參數b錯誤',action='append')

4、choices:只能選擇列表中特定的值
rq.add_argument('c',choices=['男','女'])
5、type:描述參數應該匹配的類型,可以使用python的標準數據類型string、int,也可使用Flask-RESTful提供的檢驗方法,還可以自己定義

  • 標準類型

rp.add_argument('a', type=int, required=True, help='missing a param', action='append')

  • Flask-RESTful提供
    檢驗類型方法在flask_restful.inputs模塊中

    • url
    • regex(指定正則表達式)
    • natural 自然數0、1、2、3…
    • positive 正整數 1、2、3…
    • int_range(low ,high) 整數范圍
    • rq.add_argument('d',type=inputs.int_range(1,10))
    • boolean
  • 自定義

def mobile(mobile_str):if re.match(r'^1[3-9]\d{9}$', mobile_str):return mobile_strelse:raise ValueError('{} is not a valid mobile'.format(mobile_str))rq.add_argument('e', type=mobile)


6、location:描述參數應該在請求數據中出現的位置
rq.add_argument('f',type=inputs.boolean,location='form')

from flask import Flask,Blueprint,request from flask_restful import Resource,Api,reqparse,inputsapp=Flask(__name__)#步驟1:創建restful的API api=Api(app)#步驟二:定義資源resource class HelloResource(Resource):def post(self):#1、 創建RequestParser對象rq=reqparse.RequestParser()#2、向RequestParser對象中添加需要檢驗或轉換的參數聲明# required=True:表示參數a必須要傳;type=int:表示參數必須是整形rq.add_argument('a',type=int,required=True,help='參數a錯誤') #如果定義help,那么所有的校驗都是同一個錯誤提示#3、使用parse_args()方法啟動檢驗處理req=rq.parse_args()#4、校驗完成之后獲得參數的結果,可按照字典操作或對象屬性操作a=req.areturn {'hello': 'post','a':a}#步驟3:將資源添加到api中,才可以發布 api.add_resource(HelloResource,'/hello')if __name__ == '__main__':app.run(debug=True)

上述測試代碼塊

from flask import Flask,Blueprint,request from flask_restful import Resource,Api,reqparse,inputs import re app=Flask(__name__)#步驟1:創建restful的API api=Api(app)#步驟二:定義資源resource class HelloResource(Resource):def get(self):return {'hello': 'get'}def put(self):return {'hello': 'put'}def post(self):#1、 創建RequestParser對象rq=reqparse.RequestParser()#2、向RequestParser對象中添加需要檢驗或轉換的參數聲明# required=True:表示參數a必須要傳;type=int:表示參數必須是整形rq.add_argument('a',type=int,required=True,help='參數a錯誤') #如果定義help,那么所有的校驗都是同一個錯誤提示#rq.add_argument('a',help='參數a錯誤') #如果定義help,那么所有的校驗都是同一個錯誤提示rq.add_argument('b',type=str,required=True,help='參數b錯誤',action='append')rq.add_argument('c',choices=['男','女'])rq.add_argument('d',type=inputs.int_range(1,10))def mobile(mobile_str):if re.match(r'^1[3-9]\d{9}$', mobile_str):return mobile_strelse:raise ValueError('{} is not a valid mobile'.format(mobile_str))rq.add_argument('e', type=mobile)rq.add_argument('f',type=inputs.boolean,location='form')rq.add_argument('g',type=inputs.boolean,location='args')rq.add_argument('h',type=inputs.boolean,location='headers')rq.add_argument('i',type=inputs.boolean,location='cookies')rq.add_argument('j',type=inputs.boolean,location='json')rq.add_argument('k',type=inputs.boolean,location='files')#3、使用parse_args()方法啟動檢驗處理req=rq.parse_args()#4、校驗完成之后獲得參數的結果,可按照字典操作或對象屬性操作a=req.ab=req.bc=req.cd=req.de=req.ef=req.fg=req.gh=req.hi=req.ij=req.jk=req.kreturn {'hello': 'post','a':a,'b':b,'c':c,'d':d,'e':e,'f':f,'g':g,'h':h,'i':i,'j':j,'k':k}#步驟3:將資源添加到api中,才可以發布 api.add_resource(HelloResource,'/hello')if __name__ == '__main__':app.run(debug=True)

總結

以上是生活随笔為你收集整理的【Flask】Request和RequestParser类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。