Rectangle 27 1

flask Python 'dict' object not callable?


return jsonify(user=100)

@rksh I presume that the trailing 1 in your commend is a typo?

Hi when I try to run the correct code it's not returning any output, return jsonify(user=user_id)1 it doesn't return anything. However return jsonify(user='text') returns the value

While it's ok to return a string from a Flask handler with json.dumps, the preferred way is to use jsonify, which adds the correct Content-Type and accepts a variety of arguments, e.g. you can just call:

Note
Rectangle 27 1

flask Python 'dict' object not callable?


import json

def your_function(..):
    ...
    return json.dumps(return_id)

You are not returning a JSON string

I am using Flask on python 2.7 and I want to return a value as a json value

PS: As correctly pointed out by @bereal in his answer, the above approach wont add Content-Type: application/json header in the response. You'd want to use jsonify(..) as shown in his answer.

You are returning a python object. When Flask sees a python object, it tries to call it. When you return a dictionary, it assumes it is a callable and tries to call it.

Note