frod_junior
frod_junior

Reputation: 115

TypeError: ' class ' object is not callable flask api

I am trying to build an REST API using Flask with a MONGO DB , I faced this problem when I tried to use "flask_restplus"

that's the error :

[2018-03-16 15:22:12,854] ERROR in app: Exception on /bill [GET]
Traceback (most recent call last):
  File "/home/heythem/PycharmProjects/webAPI/venv/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/heythem/PycharmProjects/webAPI/venv/lib/python3.5/site-packages/flask/app.py", line 1615, in full_dispatch_request
    return self.finalize_request(rv)
  File "/home/heythem/PycharmProjects/webAPI/venv/lib/python3.5/site-packages/flask/app.py", line 1630, in finalize_request
    response = self.make_response(rv)
  File "/home/heythem/PycharmProjects/webAPI/venv/lib/python3.5/site-packages/flask/app.py", line 1740, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/home/heythem/PycharmProjects/webAPI/venv/lib/python3.5/site-packages/werkzeug/wrappers.py", line 921, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/home/heythem/PycharmProjects/webAPI/venv/lib/python3.5/site-packages/werkzeug/wrappers.py", line 59, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/home/heythem/PycharmProjects/webAPI/venv/lib/python3.5/site-packages/werkzeug/test.py", line 923, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'Bill' object is not callable

 

@app.route('/bill')
class Bill(Resource):
    def get(self):
        bill = mongo.db.bill

        output = []
        for q in bill.find():
            output.append({
                'monitor_id': q['monitor_id'],
                'Bill_id': q['Bill_id'],
                'Amount': q['Amount']

            })
        print(output)
        return json_util.dumps({'result': output})

when I tried to test it as flask-restful API with :

api = Api(app)

I started to get that error

Upvotes: 0

Views: 1238

Answers (2)

Sundeep
Sundeep

Reputation: 1606

There are two objects, app and api

The subtle difference here is to use @api.route() not @app.route()

The routes should be defined on the flask_restplus's Api not flask's Flask for this to work.

Upvotes: 2

Arkady
Arkady

Reputation: 15079

Instead of decorating your class with @app.route(), use @api.resource()

Upvotes: 2

Related Questions