The issue is that flask is expecting a simple string or callable object in the responses, as it requires passing the values around. A dictionary is not a callable object, and you have to convert it to a form flask can pass around using either json.dumps
and setting the appropriate Content-type headers ('application/json'
in this case) or just using jsonify
that does the hard work for you. Jsonify Docs
import flask
from flask import request, jsonify #import jsonify
from collections import defaultdict
app = flask.Flask(__name__)
app.config["DEBUG"] = True
lis = [1, 2, 3, 4]
dict1 = defaultdict(list)
dict1.setdefault("key", []).append("Hello") #you do not need to setdefault if you're already using a defaultdict, but this does not raise any errors.
dict2 = {"a":"res"}
@app.route("/list/<string:index>")
def l(index):
return lis[index]
@app.route("/dictionary/<string:key>")
def d(key):
return jsonify(dict1[key]) #use it to handle dictionary
app.run()