“message”: “请求的 URL 不允许该方法。”

huangapple go评论65阅读模式
英文:

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, &#39;/api/v1/students/&lt;int:user_id&gt;&#39;)

答案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 &lt;your_url&gt; -X DELETE -v

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

huangapple
  • 本文由 发表于 2023年2月14日 03:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定