0
0
Flaskframework~5 mins

Custom status codes in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom status code in Flask?
A custom status code is a user-defined HTTP response code sent from a Flask route to indicate a specific result or error beyond standard codes.
Click to reveal answer
beginner
How do you return a custom status code from a Flask route?
Return a tuple from the route with the response body and the custom status code number, e.g., return 'Error', 499.
Click to reveal answer
intermediate
Why might you use a custom status code in a Flask app?
To communicate specific application states or errors not covered by standard HTTP codes, helping clients handle responses better.
Click to reveal answer
intermediate
Can Flask recognize custom status codes automatically?
Flask allows any integer as a status code, but custom codes are not standard and may not be recognized by browsers or tools.
Click to reveal answer
beginner
Show a simple Flask route example returning a custom status code 450.
from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
    return 'Custom status', 450
Click to reveal answer
How do you specify a custom status code in a Flask route return?
ASet a global variable with the status code
BReturn a tuple with response and status code, e.g., return 'OK', 299
CUse a decorator to define the status code
DFlask does not support custom status codes
What happens if you use a non-standard status code in Flask?
AFlask converts it to 200 OK
BFlask will reject the response
CThe code is sent but may not be understood by browsers
DThe server crashes
Which of these is a valid way to return a custom status code 499 in Flask?
Areturn 'Error', 499
Breturn 'Error', status=499
Creturn Response('Error', status_code=499)
Dreturn 'Error'.status(499)
Why should you be careful using custom status codes?
AThey might confuse clients or tools expecting standard codes
BThey always cause server errors
CThey are slower to send
DThey require special Flask plugins
Which HTTP status code range is typically used for custom codes?
A100-199
B200-299
C400-499
DThere is no official range for custom codes
Explain how to return a custom status code in a Flask route and why you might do it.
Think about how Flask returns responses and why custom codes help.
You got /3 concepts.
    Describe potential issues when using custom status codes in Flask applications.
    Consider how browsers and APIs handle unknown codes.
    You got /3 concepts.