26

How do I go about returning json data from a bottle request handler. I see a dict2json method in the bottle src but I am not sure how to use it.

What is in the documentation:

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

Gives me this when I bring up the page:

<html>
    <head></head>
    <body>statusservertime</body>
</html>
arinte
  • 3,408
  • 8
  • 41
  • 63
  • 1
    I moved on to flask and it works fine. – arinte Sep 20 '10 at 19:57
  • You can also use import json then json.dumps(dict). But good move, I also go between bottle and flask and end up choosing flask. I like bottle being lightweight and not having a bigger framework behind it. But stuff like typed url params always win me back for doing things like date detection or indexes i.e. /blog///// Where bottle only has :param names. So a path with 4 slashes isn't always a date/blog type url. I use both but tend to flask for bigger apps currently due to stuff like this. – Ryan Christensen Jan 05 '11 at 18:45
  • I have this problem too. When I use curl -I, I see that the content type is wrong: Content-Type: text/html; charset=UTF-8 – Sridhar Sarnobat Apr 23 '13 at 22:04

5 Answers5

44

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

stormdrain
  • 7,910
  • 4
  • 35
  • 74
Andrew
  • 11,325
  • 2
  • 23
  • 18
6

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
David M.
  • 3,647
  • 22
  • 26
  • This worked great for me to return arrays of dictionaries, which aren't handled by Bottle – Martín Coll May 11 '13 at 18:27
  • 1
    You shouldn't be returning a list of dictionaries, which is why bottle makes it so difficult (and Flask as well). See here: http://flask.pocoo.org/docs/security/#json-security – Martin Konecny Jul 24 '13 at 14:44
  • Returning lists of dictionaries is good now. http://flask.pocoo.org/docs/security/#json-security – Prof. Falken Jul 09 '18 at 23:44
3

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

This works:

import time
from bottle import route, run

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

run(host='localhost', port=8080)
Tim McNamara
  • 16,863
  • 3
  • 48
  • 77
0

try this should works as intended

from bson.json_util import dumps
from bottle import route, run
import time

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

run(host='localhost', port=8080)
Darshan J
  • 311
  • 2
  • 4
0

it is easy to get json using bottle's request module

from bottle import request

json_data = request.json # json_data is in the dictionary format
llcao
  • 101
  • 3