英文:
Python Flask-RESTful method delete error - "message": "The method is not allowed for the requested URL."
问题
以下是代码部分的翻译:
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()
class AddDeleteStudent(Resource):
def delete(self, user_id):
return delete_student(user_id)
api.add_resource(AddDeleteStudent, '/api/v1/students/<int:user_id>')
英文:
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>')
答案1
得分: 1
以下是翻译好的部分:
- "You didn't add code showing how you're invoking the API." --> "您没有添加显示如何调用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." --> "如果您正在使用curl,并且在调用中未指定方法,我认为它默认为
get
,而您的API没有定义get
。" - "To do a delete, you have to do something like" --> "要执行删除操作,您需要执行类似以下的操作:"
- "curl <your_url> -X DELETE -v" --> "curl <your_url> -X DELETE -v"
- "See the documentation for how to invoke and specify your method (in this case 'delete')" --> "请参阅文档,了解如何调用和指定您的方法(在这种情况下是'delete')。"
- "1: https://flask-restful.readthedocs.io/en/latest/quickstart.html#a-minimal-api" --> "1: https://flask-restful.readthedocs.io/en/latest/quickstart.html#a-minimal-api"
英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论