Question 26
The following Flask-RESTful API code snippet is intended to create a RESTful API for managing student registrations in an IITM BS degree program. However, there is an error in the code that prevents it from functioning correctly.
from flask import Flask, jsonify, requestfrom flask_restful import Api, Resource
app = Flask(__name__)api = Api(app)
students = []
class Student(Resource): def get(self, student_id): for student in students: if student["id"] == student_id: return jsonify(student) return jsonify({"message": "Student not found"}), 404
def post(self): data = request.get_json() new_student = { "id": data["id"], "name": data["name"] } students.append(new_student) return jsonify({"message": "Student added successfully"})
api.add_resource(Student, "/student/<int:student_id>")
if __name__ == "__main__": app.run(debug=True)Error:
When trying to use the POST method to add a student, it throws a TypeError stating that the route does not match the method. Identify the correct option to fix the error in the code.