HOWTO · Flask

How to Send a POST Request in Flask

Learn how to handle GET and POST requests in Flask, submit an HTML form, and read its data.

On this page

GET and POST are HTTP methods with different request semantics. A GET request asks the server for a representation and commonly carries query parameters in the URL. A POST request sends data in the request body and is commonly used when a form submits data. POST does not encrypt or conceal that data: HTTPS protects it while it travels between the client and server, while clients, the server, and any logging systems may still access it. Use HTTPS and appropriate application safeguards for sensitive information.

This example keeps the route, form field, and lookup key consistent. It serves a form at /login; submitting the form sends the name field to that same route, where Flask reads it from request.form.

Understand GET and POST in Flask

Opening a page in a browser normally sends GET. A form can use POST when its submission is intended to send data for processing. These method names describe how the request is used; they are not security settings. Query strings can appear in browser history and logs, but putting values in a POST body does not itself provide confidentiality or prevent logging.

Flask makes the incoming method available as request.method. A route that handles both methods can show the form for GET and process its submitted value for POST. The browser and server still communicate over the same transport; configure HTTPS separately for protection in transit.

Create a Flask route that accepts GET and POST

The methods argument tells Flask which methods the route accepts. In the handler, branch on request.method: return the form for GET, and handle submitted data for POST. This avoids sending a form submission to a separate endpoint and makes the route behavior easy to follow.

Build and submit the HTML form

The form’s method="post" selects POST, and the input’s name="name" becomes the key Flask uses to retrieve its value. The form action points to /login, matching the route below. A submit button sends the form; no JavaScript is needed for this basic exchange.

Read submitted form data

For a POST request, request.form.get("name", "") retrieves the submitted field and provides a fallback if it was omitted. The response escapes the value before inserting it into HTML, so text such as markup is displayed as text rather than interpreted as page markup. For a real account or data-changing workflow, add the appropriate validation, authentication, authorization, and CSRF protection; choosing POST alone does not provide those controls.

Run and verify the example

Install Flask in your environment with python -m pip install Flask, save the following as app.py, and start the development server with python -m flask --app app run. The supplied runtime record shows the Python example completed its configured success case with exit code 0 and no stdout; it does not record a browser request or server response.

from flask import Flask, request
from markupsafe import escape

app = Flask(__name__)


@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        name = request.form.get("name", "")
        return f"<h1>Hello, {escape(name)}</h1><a href='/login'>Back</a>"

    return """<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>Login form</title>
  <h1>Hello, this is the login page!</h1>
  <form action="/login" method="post">
    <label>Name: <input type="text" name="name"></label>
    <button type="submit">Submit</button>
  </form>
</html>"""

Open http://127.0.0.1:5000/login to see the form. Enter Ada and submit it; the browser sends POST and the response displays Hello, Ada. Use the browser’s developer tools Network panel to confirm the initial GET and the form’s POST, and inspect the request method and form data. The POST body is still visible to the browser and server. The development server is for local learning; use a production WSGI server and HTTPS for deployment.