Python Flask, TypeError: ‘dict’ object is not callable

Flask only expects views to return a response-like object. This means a Response, a string, or a tuple describing the body, code, and headers. You are returning a dict, which is not one of those things. Since you’re returning JSON, return a response with the JSON string in the body and a content type of application/json.

return app.response_class(rety.content, content_type="application/json")

In your example, you already have a JSON string, the content returned by the request you made. However, if you want to convert a Python structure to a JSON response, use jsonify:

data = {'name': 'davidism'}
return jsonify(data)

Behind the scenes, Flask is a WSGI application, which expects to pass around callable objects, which is why you get that specific error: a dict isn’t callable and Flask doesn’t know how to turn it into something that is.

Leave a Comment