Search code examples
pythonapiflaskurlflask-restful

Python Flask-RESTful method delete error - "message": "The method is not allowed for the requested URL."


What could be the problem?

I have a function that deletes a record from the database by id. Function separately from api works.

def delete_student(student_id: int):
    del_student = db.session.query(Student).filter_by(id=student_id).one()
    db.session.delete(del_student)
    return db.session.commit()

But when i use this function via api i get error: {"response": {"message": "The method is not allowed for the requested URL."}}

class AddDeleteStudent(Resource):
    def delete(self, user_id):
        return delete_student(user_id)

api.add_resource(AddDeleteStudent, '/api/v1/students/<int:user_id>')


Solution

  • You didn't add code showing how you're invoking the API.

    If you're using curl and you don't specify the method in the call, I believe it defaults to a get and you don't have a get defined for your API.

    To do a delete, you have to do something like

    curl <your_url> -X DELETE -v

    See the documentation for how to invoke and specify your method (in this case 'delete')