0
0
Flaskframework~20 mins

Flask route as API endpoint - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask API Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask API endpoint?
Consider this Flask route that returns JSON data. What will the client receive when accessing /api/data?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/api/data')
def data():
    return jsonify({'name': 'Alice', 'age': 30})
A{'name': 'Alice', 'age': 30}
B<Response 200 OK>
Cname: Alice, age: 30
D{"name": "Alice", "age": 30}
Attempts:
2 left
💡 Hint
Think about what jsonify does and what format the client receives.
📝 Syntax
intermediate
2:00remaining
Which Flask route definition will cause a syntax error?
Identify the Flask route decorator that will cause a syntax error when defining an API endpoint.
A
@app.route('/api/user')
def user():
return 'User data'
B
@app.route('/api/user')
def user():
    return 'User data'
C
'atad resU' nruter    
:)(resu fed
)'resu/ipa/'(etuor.ppa@
D
@app.route('/api/user')
def user():
    return {'user': 'Bob'}
Attempts:
2 left
💡 Hint
Check the indentation of the function body.
state_output
advanced
2:00remaining
What is the response status code of this Flask API endpoint?
Given this Flask route, what status code will the client receive when accessing /api/status?
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/api/status')
def status():
    return jsonify({'status': 'ok'}), 201
A201
B500
C404
D200
Attempts:
2 left
💡 Hint
Look at the second value returned by the function.
🔧 Debug
advanced
2:00remaining
Why does this Flask API endpoint raise a TypeError?
Examine the code below. Why will calling /api/error cause a TypeError?
Flask
from flask import Flask
app = Flask(__name__)

@app.route('/api/error')
def error():
    return {'message': 'fail'}
ARoute decorator syntax is invalid
BMissing return statement causes NoneType error
CFlask cannot convert dict to response automatically without jsonify
DFunction name 'error' is reserved and causes conflict
Attempts:
2 left
💡 Hint
Flask needs a proper response object or string, not a raw dict.
🧠 Conceptual
expert
3:00remaining
Which option correctly implements a Flask API endpoint that accepts a URL parameter and returns it in JSON?
You want to create an API endpoint /api/item/<id> that returns the id parameter as JSON. Which code snippet does this correctly?
A
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/api/item/&lt;id&gt;')
def item():
    return jsonify({'id': id})
B
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/api/item/&lt;id&gt;')
def item(id):
    return jsonify({'id': id})
C
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/api/item/&lt;int:id&gt;')
def item(id):
    return {'id': id}
D
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/api/item/&lt;int:id&gt;')
def item():
    return jsonify({'id': id})
Attempts:
2 left
💡 Hint
Check the function parameters and route variable types carefully.