[英]Flask + flask-restful gives strange error
I've used Flask in a couple of projects, but never used the flask-restful package when creating restul APIs - so I thought I should give it a try. 我在几个项目中使用过Flask,但在创建restul API时从未使用过flask-restful包 - 所以我想我应该试一试。
However, I've come across a weird error that I can't understand - and the API doesn't work at all. 但是,我遇到了一个我无法理解的奇怪错误 - 而API根本不起作用。 The error message says
错误消息说
{
"message": "Not Found. You have requested this URI [/api/v1.0/items] but did you mean /api/v1.0/items ?",
"status": 404
}
run.py run.py
from my_project import app
if __name__ == '__main__':
app.run(host=0.0.0.0, debug=app.config['DEBUG'])
my_project/_ init _.py my_project / _ init _.py
from flask import Flask
app = Flask(__name__)
from my_project.base import blueprint as BaseBluePrint
from my_project.api import blueprint as ApiBluePrint
app.register_blueprint(BaseBluePrint)
app.register_blueprint(ApiBluePrint, url_prefix='/api/v1.0')
my_project/api/_ init _.py my_project / api / _ init _.py
from flask import Blueprint
from flask.ext import restful
blueprint = Blueprint('api', __name__)
api = restful.Api(blueprint)
class ItemList(restful.Resource):
def get(self):
return false
api.add_resource(ItemList, '/items', endpoint='items')
What can cause this? 是什么导致这个? Whatever I do, the error remains.
无论我做什么,错误仍然存在。 Looking at the
url_map
from Flask, I can see my route in there - and it looks fine. 看看Flask的
url_map
,我可以在那里看到我的路线 - 它看起来很好。 When moving away from blueprints and keeping it all in one file, it works. 远离蓝图并将其全部保存在一个文件中时,它可以正常工作。 Python v2.7 and flask-restful v0.2.12 (installed from pip on Ubuntu Precise).
Python v2.7和flask-restful v0.2.12(从Ubuntu Precise上的pip安装)。
I had similar problems related to trailing slashes, eg i defined api.com/endpoint and was sending request to api.com/endpoint/ and was having 404 errors. 我遇到了与尾部斜杠相关的类似问题,例如我定义了api.com/endpoint并向api.com/endpoint/发送请求并且有404错误。
Solution was to configure Flask application not to be strict about trailing slashes by: app.url_map.strict_slashes = False
解决方案是配置Flask应用程序不要严格关于尾部斜杠:
app.url_map.strict_slashes = False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.