0
0
Flaskframework~10 mins

JSON response formatting in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return a JSON response using Flask.

Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/')
def home():
    return [1]({'message': 'Hello, world!'})
Drag options to blanks, or click blank then click option'
Amake_response
Bjson
Crender_template
Djsonify
Attempts:
3 left
💡 Hint
Common Mistakes
Using json instead of jsonify causes errors.
Using render_template returns HTML, not JSON.
2fill in blank
medium

Complete the code to set the HTTP status code to 201 in the JSON response.

Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/create')
def create():
    return jsonify({'result': 'Created'}), [1]
Drag options to blanks, or click blank then click option'
A201
B200
C400
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 means OK but not specifically created.
Using 400 or 404 means error, not success.
3fill in blank
hard

Fix the error in returning a JSON response with a custom header.

Flask
from flask import Flask, jsonify, make_response
app = Flask(__name__)

@app.route('/custom')
def custom():
    response = make_response(jsonify({'status': 'ok'}))
    response.headers['X-Custom-Header'] = [1]
    return response
Drag options to blanks, or click blank then click option'
ACustomValue
B'CustomValue'
C123
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes causes a NameError.
Using None is invalid for header values.
4fill in blank
hard

Fill both blanks to return a JSON response with a 404 status and a custom error message.

Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/notfound')
def not_found():
    return jsonify({'error': [1]), [2]
Drag options to blanks, or click blank then click option'
A'Resource not found'
B404
C200
D'Success'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 status code for errors is incorrect.
Not quoting the error message causes syntax errors.
5fill in blank
hard

Fill all three blanks to create a JSON response with a nested dictionary, a 202 status code, and a custom header.

Flask
from flask import Flask, jsonify, make_response
app = Flask(__name__)

@app.route('/process')
def process():
    data = {'task': [1], 'status': 'processing'}
    response = make_response(jsonify(data), [2])
    response.headers['X-Task-ID'] = [3]
    return response
Drag options to blanks, or click blank then click option'
A'upload'
B202
C'12345'
D'done'
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers without quotes for strings causes errors.
Using wrong status codes changes response meaning.