0
0
Flaskframework~20 mins

How Flask processes HTTP requests - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Request Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Flask route return?
Consider this Flask app code. What is the output when you visit /hello in a browser?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/hello')
def hello():
    return 'Hello, Flask!'

if __name__ == '__main__':
    app.run()
AThe browser shows a blank page
BThe browser shows a 404 Not Found error
CThe browser shows a 500 Internal Server Error
DThe browser shows the text: Hello, Flask!
Attempts:
2 left
💡 Hint
Check the route decorator and the return value of the function.
state_output
intermediate
2:00remaining
What is the value of request.method inside this route?
Given this Flask route, what will request.method be if you send a POST request to /submit?
Flask
from flask import Flask, request
app = Flask(__name__)

@app.route('/submit', methods=['GET', 'POST'])
def submit():
    return f'Method used: {request.method}'

if __name__ == '__main__':
    app.run()
APUT
BGET
CPOST
DDELETE
Attempts:
2 left
💡 Hint
Look at the HTTP method used in the request and the allowed methods in the route.
📝 Syntax
advanced
2:30remaining
Which option correctly defines a Flask route that accepts a dynamic integer parameter?
You want a route that matches URLs like /user/42 where 42 is a number. Which code snippet correctly defines this route?
A
@app.route('/user/<id:int>')
def user(id):
    return f'User {id}'
B
@app.route('/user/<int:id>')
def user(id):
    return f'User {id}'
C
@app.route('/user/<int>')
def user(id):
    return f'User {id}'
D
@app.route('/user/<id>')
def user(int):
    return f'User {int}'
Attempts:
2 left
💡 Hint
Flask uses syntax for dynamic parts in routes.
🔧 Debug
advanced
2:30remaining
Why does this Flask app raise a RuntimeError?
This Flask app code raises a RuntimeError: Working outside of application context. Why?
Flask
from flask import Flask, current_app
app = Flask(__name__)

print(current_app.name)

if __name__ == '__main__':
    app.run()
ABecause current_app is accessed outside a request or app context
BBecause Flask app is not created correctly
CBecause print statement is inside the if __name__ == '__main__' block
DBecause app.run() is missing parentheses
Attempts:
2 left
💡 Hint
current_app needs an active application context to work.
🧠 Conceptual
expert
3:00remaining
What is the correct order Flask processes an incoming HTTP request?
Arrange these steps in the order Flask processes an HTTP request internally.
A2,1,3,4
B1,2,3,4
C2,3,1,4
D1,3,2,4
Attempts:
2 left
💡 Hint
Flask needs a context before matching routes and running code.