0
0
Flaskframework~5 mins

Flask route as API endpoint - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a Flask route?
A Flask route is a URL pattern that tells Flask which function to run when a user visits that URL. It connects web addresses to Python functions.
Click to reveal answer
beginner
How do you define a route in Flask to respond to a GET request?
Use the @app.route decorator with the URL path and set methods=['GET']. For example:<br>@app.route('/api/data', methods=['GET'])
Click to reveal answer
beginner
What does the function decorated by a Flask route return?
It returns the response sent to the client. This can be a string, JSON data, or an HTML page.
Click to reveal answer
intermediate
How do you return JSON data from a Flask route?
Use Flask's jsonify function to convert Python data to JSON and send it as a response.<br>Example:<br>return jsonify({'key': 'value'})
Click to reveal answer
intermediate
Why is it important to specify HTTP methods in Flask routes?
Specifying methods like GET or POST tells Flask which types of requests the route can handle. This helps organize API behavior and improves security.
Click to reveal answer
Which decorator is used to create a route in Flask?
A@app.route
B@app.endpoint
C@app.url
D@app.path
How do you specify that a Flask route should only accept POST requests?
Amethods=['POST'] in @app.route
Bmethod='POST' in @app.route
Cpost=True in @app.route
DOnly by naming the function post_
What does the Flask function jsonify do?
ADefines a new route
BConverts Python data to JSON response
CStarts the Flask server
DHandles form data
If you want a route to respond to both GET and POST requests, how do you specify it?
Amethods='GET, POST'
Bmethods='both'
Cmethods=['GET POST']
Dmethods=['GET', 'POST']
What will happen if you visit a Flask route URL that only accepts POST but you send a GET request?
AFlask returns a 404 Not Found error
BFlask runs the route anyway
CFlask returns a 405 Method Not Allowed error
DFlask crashes
Explain how to create a simple Flask API endpoint that returns JSON data when accessed via a GET request.
Think about how you connect a URL to a function and send back JSON.
You got /3 concepts.
    Describe why specifying HTTP methods in Flask routes is important for API design.
    Consider what happens if you don't limit request types.
    You got /4 concepts.