Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 72 ยท Routing and Views

Routes and HTTP Methods

Learn how Flask routes work, how to accept GET and POST with the methods argument, use the app.get and app.post shortcuts, and understand 405 Method Not Allowed.

A route connects a URL path to a Python function. When a request arrives, Flask looks through its registered routes, finds the one that matches the path and the HTTP method, and calls the function. You have already written routes with @app.route; now we look at how matching works and how to control which HTTP methods a route accepts.

Methods matter because they describe intent. A browser asking for a page sends GET. A form that creates something sends POST. An API client may send PUT to replace data or DELETE to remove it. Treating these differently is the foundation of forms, REST APIs and safe web applications.

The route decorator

@app.route(rule) takes a rule, the URL path pattern, and registers the decorated function as a view. The same function can have several rules, and a rule must start with a slash.

Python Routes are rules in a table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each rule shows the methods it accepts. Without a methods argument a route accepts only GET, and Flask adds HEAD (the same as GET without a body) and OPTIONS (a description of what is allowed) automatically.

Matching a request

flowchart TD
    R["Request:<br/>method + path"] --> P{"Does any rule<br/>match the path?"}
    P -->|no| N["404 Not Found"]
    P -->|yes| M{"Does that rule<br/>allow the method?"}
    M -->|no| E["405 Method Not Allowed<br/>+ Allow header"]
    M -->|yes| V["Call the view"]

A path that exists but is requested with the wrong method gets 405, not 404.

This distinction is useful for debugging. A 404 means "no route for that path"; a 405 means "the path is right, but this route does not accept that method".

Accepting more than one method

Pass a list to methods to accept more verbs. Inside the view, request.method tells you which one arrived. The classic pattern is one URL that shows a form on GET and handles the submission on POST:

Python One URL, two methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The test client's post(..., data={...}) sends a form body, which request.form reads (the Request Object lesson covers this fully). The PUT at the end is rejected, because PUT is not in the list.

Note: Method names are case-insensitive in the list, but writing them in capitals ("GET", "POST") is the convention.

The get and post shortcuts

Since Flask 2.0 you can write @app.get, @app.post, @app.put, @app.patch and @app.delete instead of passing methods. They register the function for exactly that method (and GET routes also get HEAD and OPTIONS). They shine when the same URL needs a different function per method, which keeps each view short:

Python Separate views per method

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The three views share the URL /notes but have unique function names, which is required. A single @app.route("/notes", methods=[...]) function with if branches also works, but separate functions are easier to read and to test as the logic grows.

GET and POST in practice

HTTP (HyperText Transfer Protocol) is the request-and-response conversation between a browser and a server: the client sends a method, a path, headers and sometimes a body, and the server answers with a status code, headers and a body. The two methods you will meet most often behave differently:

GET POST
Purpose read a page or data send data to be processed
Where the data travels in the URL (?q=flask&page=2) in the request body
Visible in history and logs yes the body is not in the URL
Can be bookmarked and cached yes no
Safe to repeat yes (it should change nothing) no (it may create a second order)

Both can carry data, so the real rule is about meaning: never use GET for something that changes state, and never put passwords or tokens in a URL. The same route can answer both methods, and the code can tell them apart with request.method. Query-string values are in request.args and form-body values are in request.form (The Request Object).

Python The same path, GET and POST

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The GET calls carry limit in the URL and change nothing. The POST calls carry number in the body and each one changes the server state, so repeating them would store the value again. A method that is not listed, such as PUT here, gets 405.

405 Method Not Allowed

When the path matches but the method does not, Flask answers 405 and adds an Allow header that lists what is accepted. Browsers show this when you open a POST-only URL by typing it in the address bar (that sends a GET).

Python Reading a 405 response

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the last two requests: OPTIONS and HEAD work on /status without you writing anything. HEAD returns the same headers as GET with an empty body, which tools use to check that a URL exists.

Safe and unsafe methods

HTTP defines which methods are safe (they must not change anything on the server) and which are idempotent (repeating them has the same effect as doing them once).

Method Typical use Safe Idempotent
GET Read a page or resource Yes Yes
POST Create something, submit a form No No
PUT Replace a resource No Yes
PATCH Change part of a resource No No
DELETE Remove a resource No Yes

The practical rule for web pages: never change data in a GET view. Browsers, caches and search crawlers send GET requests freely (prefetching links, retrying, indexing), so a link like /delete/7 can be triggered by accident. Use POST (or DELETE for APIs) for anything that modifies data.

Warning: HTML forms can only send GET and POST. To delete or update from a plain HTML page, submit a POST form to a dedicated route; use PUT and DELETE from JavaScript or API clients.

Naming and ordering

Rules are matched by specificity, not by the order you wrote them, so a fixed path like /notes/new wins over a variable path like /notes/<id> (variables are the subject of the next lesson). Still, keep related routes together in your file and give views clear names: the function name becomes the endpoint that url_for uses to build links.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which methods does @app.route("/page") accept when methods is not given?

    A route defaults to GET; Flask adds HEAD and OPTIONS automatically.
  2. 2. A browser requests a URL that only accepts POST. What does Flask return?

    The path exists but the method is not allowed, so Flask sends 405 along with an Allow header listing the accepted methods.
  3. 3. What is the difference between 404 and 405?

    Flask checks the path first. A missing path is 404; a known path with a method it does not accept is 405.
  4. 4. Why should a view never delete data when handling a GET request?

    Safe methods are expected not to change anything. Prefetching, retries and crawlers can trigger GET requests unexpectedly.
  5. 5. Two views both use @app.get("/notes") and @app.post("/notes"). What must be true of the functions?

    Each function name becomes its endpoint, and endpoints must be unique, so the two views need different names.

Summary

  • A route maps a path and a set of methods to a view; a bare @app.route accepts GET (plus automatic HEAD and OPTIONS).
  • Use methods=["GET", "POST"] and request.method, or the @app.get, @app.post, @app.put, @app.patch and @app.delete shortcuts for separate views.
  • Wrong path gives 404; right path with the wrong method gives 405 and an Allow header.
  • GET is safe and must not modify data; use POST, PUT, PATCH or DELETE for changes.
  • The test client has a method for each verb: get, post, put, patch, delete, head and options.

Next: URL Variables and Converters, where routes start capturing values from the path.