Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bahamat
Created July 25, 2017 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bahamat/4dc808857fb39d29985bc09ee18fd8aa to your computer and use it in GitHub Desktop.
Save bahamat/4dc808857fb39d29985bc09ee18fd8aa to your computer and use it in GitHub Desktop.
Simple flask example.

REST API with Flask

See https://flask.readthedocs.org/en/0.1/quickstart/ for more.

import flask

app = flask.Flask(__name__)

@app.route('/')
def index():
    return "hello"

@app.route('/submit', methods=['POST', 'GET'])
def submit():
    return flask.request.values['param']

app.run(host='::', port=5000, threaded=True)

Test it with curl. Note that I use zsh, the trailing % means that there was no \n sent.

The root endpoint.

> curl http://localhost:5000/      
Hello%

The /submit endpoint, using the GET method and query parameters.

> curl 'http://localhost:5000/submit?param=bar' -X GET
bar%

The /submit endpoint, using the POST method and form values.

> curl http://localhost:5000/submit -X POST --form param=bar
bar%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment