最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Flask-Restful中如何获得GET的URL中的参数

Flask crifan 7049浏览 0评论

之前都是通过:

<code>        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('phone',
                                   type = str,
                                   default = "",
                                   location = 'json')

    def post(self):
        args = self.reqparse.parse_args()
        gLog.debug("args=%s", args)

        phone = args["phone"]
        gLog.debug("phone=%s", phone)
</code>

去获得POST中json中的参数的

此处需要:

获得GET时,url中key=value的参数,即query parameter

flask-restful reqparse query para

python – flask restful: passing parameters to GET request – Stack Overflow

flask-restful how to fetch GET parameter – Stack Overflow

RequestParser location

Request Parsing — Flask-RESTful 0.2.1 documentation

请求解析 — Flask-RESTful 0.3.1 documentation

“在 add_argument() 中使用 location 参数可以指定解析参数的位置。flask.Request 中任何变量都能被使用。例如:

<code># Look only in the POST body
parser.add_argument('name', type=int, location='form')

# Look only in the querystring
parser.add_argument('PageSize', type=int, location='args')

# From the request headers
parser.add_argument('User-Agent', type=str, location='headers')

# From http cookies
parser.add_argument('session_id', type=str, location='cookies')

# From file uploads
parser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')
</code>

->

此处使用

location=’args’

好像此处location默认就是’args’

【总结】

Flask-restful中想要获取url中key=value的query parameters,可以用:

location = ‘args’

即可:

<code>class UserIdAPI(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('phone',
                                   type = str,
                                   default = "",
                                   location = 'args')

    def get(self):
        args = self.reqparse.parse_args()
        gLog.debug("args=%s", args)

        phone = args["phone"]
        gLog.debug("phone=%s", phone)
</code>

即可。

转载请注明:在路上 » 【已解决】Flask-Restful中如何获得GET的URL中的参数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.170 seconds, using 22.15MB memory