Bottle and Json Bottle and Json json json

Bottle and Json


Simply return a dict. Bottle handles the conversion to JSON for you.

Even dictionaries are allowed. They are converted to json and returned with Content-Type header set to application/json. To disable this feature (and pass dicts to your middleware) you can set bottle.default_app().autojson to False.

@route('/api/status')def api_status():    return {'status':'online', 'servertime':time.time()}

Taken from the documentation.

http://bottlepy.org/docs/stable/api.html#the-bottle-class


For some reason, bottle's auto-json feature doesn't work for me. If it doesn't work for you either, you can use this decorator:

def json_result(f):    def g(*a, **k):        return json.dumps(f(*a, **k))    return g

Also handy:

def mime(mime_type):    def decorator(f):        def g(*a, **k):            response.content_type = mime_type            return f(*a, **k)        return g    return decorator


return {'status':'online', 'servertime':time.time()} works perfectly well for me. Have you imported time?

This works:

import timefrom bottle import route, run@route('/')def index():    return {'status':'online', 'servertime':time.time()}run(host='localhost', port=8080)