簡體   English   中英

python中的flask rest api中的錯誤404

[英]Error 404 in flask rest api in python

我是 Flask Restful api 的新手,我正在嘗試制作一個獲取 JSON 數據的 rest 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'

您應該按照flask-restful docs的要求將“get_tasks”方法重命名為“get”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM