Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
✗ Incorrect
The Flask class is imported from the flask module to create the app.
2fill in blank
mediumComplete the code to get the API key from request headers.
Flask
api_key = request.headers.get('[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Authorization' header instead
Using 'Content-Type' header
✗ Incorrect
The API key is usually sent in a custom header like 'X-API-KEY'.
3fill in blank
hardFix the error in the code to check if the API key matches the expected key.
Flask
if api_key != [1]: return {'error': 'Unauthorized'}, 401
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the key
Comparing api_key to itself
✗ Incorrect
The expected API key should be a string literal in quotes.
4fill in blank
hardFill both blanks to create a Flask route that requires API key authentication.
Flask
@app.route('/data', methods=[[1]]) def get_data(): api_key = request.headers.get('X-API-KEY') if api_key != 'mysecretkey': return [2], 401 return {'data': 'Here is your data'}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET
Returning success message on unauthorized
✗ Incorrect
The route should accept GET requests and return an error dictionary with 401 if unauthorized.
5fill in blank
hardFill all three blanks to complete the Flask app with API key check and run the server.
Flask
from flask import Flask, request app = Flask(__name__) @app.route('/info', methods=[[1]]) def info(): key = request.headers.get('X-API-KEY') if key != [2]: return [3], 401 return {'info': 'Secret info'} if __name__ == '__main__': app.run(debug=True)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST method
Not quoting the API key
Returning success on wrong key
✗ Incorrect
The route uses GET method, checks the API key string 'supersecret', and returns an error dictionary with 401 if unauthorized.