How to fix Python bottle Unsupported response type: <class 'dict'>
Problem:
You are running a Python HTTP server using bottle. When you access your HTTP server endpoint, you see a HTTP 500 error message like
Unsupported response type: <class 'dict'>
Solution
This occurs because you are trying to return a Python list
of dictionaries, for example in
from bottle import route, run, template, response
@route('/')
def index():
# We expect bottle to return a JSON here
# but that doesn't happen!
return [{"a": "b"}]
run(host='localhost', port=8080)
In order to work around this behaviour, you need to set response.content_type
and explicitly use json.dumps()
to convert your JSON into a string:
from bottle import route, run, template, response
import json
@route('/')
def index():
response.content_type = 'application/json'
return json.dumps([{"a": "b"}])
run(host='localhost', port=8080)