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

【已解决】Flask中获取REST API接口的传递进来的参数

Flask crifan 6888浏览 0评论

折腾:

【已解决】CentOS服务器中搭建Python的Flask的REST API

期间,需要获取Flask的rest api接口传递进来的参数

flask rest api get parameter

快速入门 — Flask 0.10.1 文档

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

可以用:request

也可以用:reqparse,但是后来Flask不推荐了

Request Parsing — Flask-RESTful 0.2.1 documentation

flask parse parameter

web services – Python Flask how to get parameters from a URL? – Stack Overflow

Request Parsing — Flask-RESTPlus 0.10.1 documentation

好像想起来了:

此处如果想要提供REST的API的话,其实是需要:

先去安装一个REST的库,比如flask-restful

然后基于此,再去讨论如何获取参数

而现在Flask的REST库,看看有没有更新什么更好的

【已解决】选择好的Flask的REST API的框架

还是去用flask-restful

flask restful get parameters

Request Parsing — Flask-RESTful 0.2.1 documentation

-》

Flask-RESTful — Flask-RESTful 0.3.6 documentation

-》

Request Parsing — Flask-RESTful 0.3.6 documentation

“Warning

The whole request parser part of Flask-RESTful is slated for removal and will be replaced by documentation on how to integrate with other packages that do the input/output stuff better (such as marshmallow). This means that it will be maintained until 2.0 but consider it deprecated. Don’t worry, if you have code using that now and wish to continue doing so, it’s not going to go away any time too soon.”

对于现在版本是0.3.6的latest的Flask-restful来说,现在正在把整个的 request parser= request parsing的模块都去掉,而该换成第三方的,更好用的,比如:marshmallow

但是在2.0版本之前,暂时你还可以继续用reqparse(其代替了更早之前的argparse)

16.4. argparse — Parser for command-line options, arguments and sub-commands — Python 3.8.0a0 documentation

去看看:

http://flask-restful.readthedocs.io/en/latest/reqparse.html

http://flask-restful.readthedocs.io/en/latest/api.html#module-reqparse

Quickstart — Flask-RESTful 0.3.6 documentation

webargs/flaskrestful_example.py at dev · sloria/webargs

先去安装flask-restful:

<code>➜  robotDemo pipenv install flask-restful
Installing flask-restful…
Collecting flask-restful
  Downloading https://files.pythonhosted.org/packages/47/08/89cf8594735392cd71752f7cf159fa63765eac3e11b0da4324cdfeaea137/Flask_RESTful-0.3.6-py2.py3-none-any.whl
Collecting aniso8601&gt;=0.82 (from flask-restful)
  Downloading https://files.pythonhosted.org/packages/ba/8c/4cd25b3facc5f443cb083f4582483e8c8e7901380c71c44aff6eeda4dc54/aniso8601-3.0.0-py2.py3-none-any.whl
Requirement already satisfied: Flask&gt;=0.8 in /Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages (from flask-restful) (0.12.2)
Collecting pytz (from flask-restful)
  Downloading https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB)
Collecting six&gt;=1.3.0 (from flask-restful)
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Requirement already satisfied: Werkzeug&gt;=0.7 in /Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages (from Flask&gt;=0.8-&gt;flask-restful) (0.14.1)
Requirement already satisfied: itsdangerous&gt;=0.21 in /Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages (from Flask&gt;=0.8-&gt;flask-restful) (0.24)
Requirement already satisfied: Jinja2&gt;=2.4 in /Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages (from Flask&gt;=0.8-&gt;flask-restful) (2.10)
Requirement already satisfied: click&gt;=2.0 in /Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages (from Flask&gt;=0.8-&gt;flask-restful) (6.7)
Requirement already satisfied: MarkupSafe&gt;=0.23 in /Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages (from Jinja2&gt;=2.4-&gt;Flask&gt;=0.8-&gt;flask-restful) (1.0)
Installing collected packages: aniso8601, pytz, six, flask-restful
Successfully installed aniso8601-3.0.0 flask-restful-0.3.6 pytz-2018.4 six-1.11.0

Adding flask-restful to Pipfile's [packages]…
Pipfile.lock (011179) out of date, updating to (09e966)…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (09e966)!
Installing dependencies from Pipfile.lock (09e966)…
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 10/10 — 00:00:02
To activate this project's virtualenv, run the following:
 $ pipenv shell
</code>

去看看安装后的依赖关系:

<code>➜  robotDemo pipenv graph
Flask-RESTful==0.3.6
  - aniso8601 [required: &gt;=0.82, installed: 3.0.0]
  - Flask [required: &gt;=0.8, installed: 0.12.2]
    - click [required: &gt;=2.0, installed: 6.7]
    - itsdangerous [required: &gt;=0.21, installed: 0.24]
    - Jinja2 [required: &gt;=2.4, installed: 2.10]
      - MarkupSafe [required: &gt;=0.23, installed: 1.0]
    - Werkzeug [required: &gt;=0.7, installed: 0.14.1]
  - pytz [required: Any, installed: 2018.4]
  - six [required: &gt;=1.3.0, installed: 1.11.0]
</code>

然后可以去写代码了:

最后用:

<code>from flask import Flask
from flask import jsonify
from flask_restful import Resource, Api, reqparse


app = Flask(__name__)
api = Api(app)

class RobotAPI(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('q', type=str)

        parsedArgs = parser.parse_args()
        print(parsedArgs)

        respDict = {
            "code": 200,
            "message": "generate answer ok",
            "data": {
                "answer": "Following will play the Chrismas song: Jingle Bells ...",
                "audio": {
                    "contentType": "audio/mpeg",
                    "name": "Jingle Bells.mp3",
                    "url": "http://1.2.3.4/a/b/c.mp3"
                }
            }
        }

        return jsonify(respDict)


api.add_resource(RobotAPI, '/qa', endpoint='qa')


if __name__ == "__main__":
    app.debug = True
    app.run()
</code>

获得了参数:

输出log:

<code>{'q': 'play a Christmas song'}
</code>

127.0.0.1 – – [18/Apr/2018 11:33:00] “GET /qa?q=play+a+Christmas+song HTTP/1.1” 200 –

转载请注明:在路上 » 【已解决】Flask中获取REST API接口的传递进来的参数

发表我的评论
取消评论

表情

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

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