我是新来的烧瓶Restful api我正在尝试制作一个获取JSON数据的休息api。虽然输入curl命令我得到404错误。这是代码可以任何人请帮助。
#flask/bin/python
from flask import Flask, jsonify, abort, request, make_response,url_for,render_template
from flask.ext.restful import Api, Resource
#from flask.ext.restful import restful, Api
app = Flask(__name__, static_folder ="\Flae\app\default")
api= Api(app)
@app.errorhandler(404)
def page_not_found(e):
return 'Sorry, Nothing at this URL.', 404
class Task(Resource):
tasks = [
{
'id': 1,
'message': u'You are notified'
},
{
'id': 2,
'message': u'You are notified'
}
]
def get_tasks():
return jsonify( { 'tasks': tasks } )
api.add_resource(Task, '/api/v1/tasks')
if __name__ == '__main__':
app.run()
显示的错误是:
C:\Users\Chaitanya>curl http://127.0.0.1:5000/
Sorry, Nothing at this URL.
C:\Users\Chaitanya>curl http://127.0.0.1:5000/api/v1/tasks
{"status": 500, "message": "Internal Server Error"}
C:\Users\Chaitanya>
生成的日志是:
C:\Users\Chaitanya\Desktop\Flae\batch-fuogy-01\default>python main.py
* Running on http://127.0.0.1:5000/
* Restarting with reloader
127.0.0.1 - - [10/Feb/2015 17:39:09] "GET /api/v1/tasks HTTP/1.1" 500 -
Traceback (most recent call last):
File "D:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "D:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "D:\Python27\lib\site-packages\flask_restful\__init__.py", line 263,in error_router
return original_handler(e)
File "D:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\Python27\lib\site-packages\flask_restful\__init__.py", line 260,in error_router
return self.handle_error(e)
File "D:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "D:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "D:\Python27\lib\site-packages\flask_restful\__init__.py", line 263, in error_router
return original_handler(e)
File "D:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "D:\Python27\lib\site-packages\flask_restful\__init__.py", line 260, in error_router
return self.handle_error(e)
File "D:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "D:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "D:\Python27\lib\site-packages\flask_restful\__init__.py", line 431, in wrapper
resp = resource(*args, **kwargs)
File "D:\Python27\lib\site-packages\flask\views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "D:\Python27\lib\site-packages\flask_restful\__init__.py", line 516, in dispatch_request
assert meth is not None, 'Unimplemented method %r' % request.method
AssertionError: Unimplemented method 'GET'
答案 0 :(得分:3)
你应该重命名' get_tasks'获得'的方法正如烧瓶文件所要求的那样:
https://flask-restful.readthedocs.org/en/0.3.1/quickstart.html#a-minimal-api