The Flask Request Object
The request object holds everything about the request that arrived. Views read it to answer well.
Make the app
This line imports Flask and request together. The route below reads the request.
from flask import Flask, request
app = Flask(__name__)Echo the method and path
The view joins the method and the path into one line of text.
@app.route("/info", methods=["GET", "POST"])
def info():
return request.method + " " + request.pathAsk with GET and POST
The same route answers both methods. Each print shows the method and the path that arrived.
client = app.test_client()
print(client.get("/info").data.decode())
print(client.post("/info").data.decode())Example
from flask import Flask, request
app = Flask(__name__)
@app.route("/info", methods=["GET", "POST"])
def info():
return request.method + " " + request.path
client = app.test_client()
print(client.get("/info").data.decode())
print(client.post("/info").data.decode())Key points
- request comes from flask, like Flask.
- request.method holds GET or POST.
- request.path holds the address that was asked for.
- request holds one request at a time.
In this example, the view reads request.method and request.path. The test client asks for /info with GET and then with POST. The prints show the method and the path of each request.
Note
Import request before using it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does request.path hold for a request to /info?