Flask routing and HTTP methods¶
In the [[Flask]] framework, routing is the mechanism used to map URLs (or endpoints) to specific Python functions, allowing the application to respond to different web requests^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md]. These routes are defined using the @app.route decorator^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
Variable Rules¶
Routes can be defined with dynamic segments, known as variable rules, to capture values from the URL^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md]. For example, a route can be configured to accept a string parameter, such as a customer ID, by using the syntax <string:variable_name>^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
@app.route("/get/<string:customerID>", methods=['GET'])
def get_customer(customerID):
# logic to retrieve customer
HTTP methods¶
Flask allows different HTTP methods to be associated with the same route URL^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md]. By default, routes respond to GET requests, but the methods argument can be used to specify others, such as POST^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
GET: Generally used for retrieving data^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].POST: Generally used for passing data to the service^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
If a client attempts to access an endpoint using an unsupported method (e.g., a browser GET request to a POST-only route), Flask returns a "Method Not Allowed" error^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
Request Handling¶
Routes handling POST requests often need to access data sent by the client^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md]. This data can be accessed using the request object from the flask library^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
To correctly parse the request body as JSON, the client must include the Content-Type: application/json header^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md]. Once the header is set, the data can be accessed via request.json^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
HTTP status codes¶
Flask functions can return specific HTTP status codes alongside the response data to indicate the result of the request^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
- 200 OK: Indicates success^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
- 400 Bad Request: Indicates that the client sent malformed or incorrect data (e.g., broken JSON format)^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
- 404 Not Found: Indicates that the requested resource (like a specific customer ID) does not exist^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
- 500 Internal Server Error: Indicates an unexpected error in the application code (e.g., a
KeyErrorwhen accessing a dictionary)^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md].
Related Concepts¶
- [[Flask]]
- [[Docker]]
- HTTP Status Codes
Sources¶
^[400-devops__09-Scripting-Language__python__introduction__part-4.http__README.md]