Complete the code to return a 200 OK status in a Flask API response.
from flask import Flask, jsonify app = Flask(__name__) @app.route('/status') def status(): return jsonify({'message': 'Success'}), [1]
The HTTP status code 200 means the request was successful.
Complete the code to return a 404 Not Found status when a resource is missing.
from flask import Flask, jsonify app = Flask(__name__) @app.route('/item/<int:id>') def get_item(id): item = None # pretend we looked for the item if not item: return jsonify({'error': 'Item not found'}), [1]
404 means the requested resource was not found on the server.
Fix the error in the code to return a 201 Created status after adding a new resource.
from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/add', methods=['POST']) def add_item(): data = request.json # pretend we add the item here return jsonify({'message': 'Item added'}), [1]
201 means a new resource was successfully created.
Fill both blanks to return a 400 Bad Request status with a JSON error message.
from flask import Flask, jsonify, request app = Flask(__name__) @app.route('/submit', methods=['POST']) def submit(): data = request.json if not data or 'name' not in data: return jsonify({'error': [1]), [2]
400 means the client sent a bad request. The error message explains the problem.
Fill all three blanks to return a 403 Forbidden status with a JSON error message and a custom header.
from flask import Flask, jsonify, make_response app = Flask(__name__) @app.route('/admin') def admin(): response = make_response(jsonify({'error': [1]), [2]) response.headers['X-Reason'] = [3] return response
403 means the user is forbidden from accessing the resource. The JSON and header explain why.