flask-restful急速体验

1. 安装,有两种安装方式

Install Flask-RESTful with pip

pip install flask-restful

The development version can be downloaded from its page at GitHub.

git clone https://github.com/flask-restful/flask-restful.git
cd flask-restful
python setup.py develop

2.使用

A Minimal API

A minimal Flask-RESTful API looks like this:

from flask import Flask
from flask_restful import Resource, Api

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

class HelloWorld(Resource):
    def get(self):
        return {'hello': 'world'}

api.add_resource(HelloWorld, '/')

if __name__ == '__main__':
    app.run(debug=True)

Save this as api.py and run it using your Python interpreter. Note that we’ve enabled Flask debugging mode to provide code reloading and better error messages.

$ python api.py
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

Warning

Debug mode should never be used in a production environment!

Now open up a new prompt to test out your API using curl

$ curl http://127.0.0.1:5000/
{"hello": "world"}

实操:
xy7joavxxzs_5cpyr5x3

4fuug_n4_fklhhq5fm