[英]HTTP Status 405 and Restful API in Flask Python
The general goal总目标
For a webshop without frameworks I am working on, I need to define a good route to the restful API of the application.对于我正在开发的没有框架的网店,我需要定义一条通往应用程序的 RESTful API 的良好路径。
The problem问题
When I load the page (for example: localhost:.../api/favorites) I get the HTTP 405 error with the message: "Method not allowed".当我加载页面(例如:localhost:.../api/favorites)时,我收到 HTTP 405 错误消息:“方法不允许”。
I have searched for different kinds of sources such as a forum post about the HTTP 405 error: HTTP Status 405 - Method Not Allowed Error for Rest API .我搜索了不同类型的资源,例如关于 HTTP 405 错误的论坛帖子: HTTP 状态 405 - Rest API 的方法不允许错误。 But I could not discover how this would solve my error.但我无法发现这将如何解决我的错误。
And I have studied the 'Flask Documentation' and have found an example:我研究了“烧瓶文档”并找到了一个例子:
@app . url_defaults
def add_language_code ( endpoint , values ) :
if lang_code in values or not g . lang_code :
return
if app . url_map . is_endpoint_expecting ( endpoint , lang_code ) :
values [ lang_code ] = g . lang_code
But I could not find what I am doing exactly wrong or is missing, and most important: why do I get that HTTP 405 error with this @api route.但是我找不到我做错了什么或遗漏了什么,最重要的是:为什么我会收到这个@api 路由的 HTTP 405 错误。
Explanation of my project:我的项目说明:
In the database I have a table named ' favorites ' with the columns ' user_id ' and ' product_id .在数据库中,我有一个名为“收藏夹”的表,其中包含“ user_id ”和“ product_id ”列。 On the /favorites page I want to load data from the favorites table to show the favorited items by a (specific) user.在 /favorites 页面上,我想从收藏夹表加载数据以显示(特定)用户收藏的项目。 But first of all I want just to import the data from the database.但首先我只想从数据库中导入数据。 I want to do this by retrieving the product_id from the database (first route).我想通过从数据库(第一条路线)中检索 product_id 来做到这一点。 And later on I want to specify that it is only possible to favorite items when you have ordered it.稍后我想说明只有在您订购后才可以收藏。 (orderhistory or combined by user_id) (second route). (orderhistory 或由 user_id 组合)(第二条路线)。
The front-end of the webshop is made with html/css/javascript and the back-end is flask by python.网店的前端是html/css/javascript,后端是python的flask。
The question:问题:
Is the error of the HTTP 405 message due to a fault in this piece of code that routes to the API? HTTP 405消息的错误是由于这段路由到API的代码中的错误导致的吗?
Could someone understand and maybe explain my precise code mistake so I can try to fix it by knowing what is causing the error?有人可以理解并解释我的确切代码错误,以便我可以通过了解导致错误的原因来尝试修复它吗?
First route第一条路线
@api.route('/favorites/<int:product_id>', methods = ['GET'])
def get_favorites(product_id):
return favorites.get_favorites(product_id)
Second route第二条路线
@api.route('/favorites/<int:user_id>')
def get_favorites_by_user_id(user_id):
return favorites.get_favorites_by_user_id(user_id)
Thank you very much in advance!非常感谢您提前!
It would be nice if you can show your front end code.如果你能展示你的前端代码就好了。 But I think you made a post request to the backend without listing methods=['GET', 'POST']
in your logic.但是我认为您在逻辑中没有列出methods=['GET', 'POST']
向后端发出了一个发布请求。 Thus getting a 405.从而得到405。
Just add the POST to your list只需将 POST 添加到您的列表中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.