之前都是通过:
<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>
即可。