HTTP defines a set of request methods (also known as verbs) indicating the desired action to be performed on a given resource. By default, @app.route() only responds to GET requests. To build interactive web forms or REST APIs, routes must explicitly accept methods.
methods Argument in @app.routeTo allow multiple HTTP verbs on a route, supply a list to the methods parameter:
If a client sends an unsupported method (e.g., DELETE to /api/v1/articles), Flask automatically responds with an HTTP 405 Method Not Allowed header containing the allowed verbs.
In RESTful design, you can either handle all methods in one view function using if request.method == '...', or separate them using modern route decorators:
| Method | Idempotent | Safe | Primary Purpose | Common Response Code |
|---|---|---|---|---|
| GET | Yes | Yes | Retrieve representation of a resource without side effects | 200 OK |
| POST | No | No | Submit data to create a new subordinate resource | 201 Created |
| PUT | Yes | No | Replace the entire target resource with payload | 200 OK / 204 No Content |
| PATCH | No | No | Apply partial modifications to a resource | 200 OK |
| DELETE | Yes | No | Remove the target resource permanently | 204 No Content |
| HEAD | Yes | Yes | Same as GET, but transfers status line and header section only | 200 OK |
| OPTIONS | Yes | Yes | Describe communication options for target resource (CORS preflight) | 204 No Content |
Flask handles HEAD and OPTIONS automatically:
GET route, Flask automatically adds HEAD support. If a HEAD request arrives, Flask invokes the route handler, computes headers, but strips the response body before sending.OPTIONS response listing allowed verbs in the Allow HTTP header.@app.route('/test') is defined without the methods argument?DELETE request to a route configured with methods=['GET', 'POST']?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Dynamic URLs & Type Converters | The Flask Request Object & Data Parsing |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.