Created
November 28, 2017 16:52
-
-
Save petronetto/0fdab5a79b662c219a27a76468efaa74 to your computer and use it in GitHub Desktop.
Simple Flask RESTful API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Flask simple API | |
------------------------------------------------- | |
1) Install dependencies with `pip install flask gunicorn` | |
2) Run with `gunicorn -b 0.0.0.0:5555 app:app --reload --capture-output` | |
""" | |
from flask import Flask, request, jsonify, abort | |
app = Flask(__name__) | |
@app.route('/', methods=['GET']) | |
def get(): | |
response = jsonify({'hello': 'I\'m a GET Method'}) | |
response.status_code = 200 | |
return response | |
@app.route('/', methods=['POST']) | |
def post_something(): | |
name = request.json.get('name', '') | |
if name: | |
response = jsonify({'hello': 'Hello %s' % name}) | |
response.status_code = 201 | |
return response | |
response = jsonify({'hello': 'Hello Guest'}) | |
response.status_code = 201 | |
return response | |
if __name__ == '__main__': | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment