HTTP Methods in Flask

Python
Flask
Web Applications
URL
HTTP
HTTP Methods
Author

Galen Seilis

Published

July 29, 2024

The hypertext transfer protocol (HTTP) is a way for a web application to respond to requests.

There are four HTTP methods that you’ll typically find in introductory material:

In this post we’ll provide a simple example. We will embed the following video

when GET is used. If the method is POST, then the page will render "POST". Otherwise the function implicitly returns None, which offhand I don’t think does much of anything.

"""Flask application demonstrating HTTP methods."""

from flask import Flask, request

app = Flask(__name__)


@app.route("/automorphisms", methods=["GET", "POST"])
def http_methods_example():
    if request.method == "GET":
        return '<iframe width="560" height="315" src="https://www.youtube.com/embed/vY1UkCPSKH8?si=mRB5eM30UmTUVfhv" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>'
    if request.method == "POST":
        return "POST"
    return ""


def main() -> None:
    app.run(debug=True)


if __name__ == "__main__":
    main()

Other than taking the parameter methods in app.route, there isn’t much to this application. We’re showing a tag for an embedded video, which for us to render only involves returning a string of the tag.