Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest: Flask echo POST

This is very similar to the previous “application” except here the form sends a “POST” request and the server handles that.

from flask import Flask, request

app = Flask(__name__)

@app.route("/")
def main():
    return '''
     <form action="/echo" method="POST">
         <input name="text">
         <input type="submit" value="Echo">
     </form>
     '''

@app.route("/echo", methods=['POST'])
def echo():
    user_text = request.form.get('text', '')
    if user_text:
        return "You said: " + user_text
    return "Nothing to say?"


Run the application

flask --app echo_post --debug run

Visit http://localhost:5000/