Bottle and Json

18,869

Solution 1

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

Solution 2

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

Solution 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)
Share:
18,869

Related videos on Youtube

arinte
Author by

arinte

Updated on August 01, 2020

Comments

  • arinte
    arinte almost 4 years

    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
      arinte almost 14 years
      I moved on to flask and it works fine.
    • Jobayer Ahmed Mickey
      Jobayer Ahmed Mickey over 13 years
      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/<int:day>/<int:month>/<int:year>/<path:path>/ 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.
    • Sridhar Sarnobat
      Sridhar Sarnobat about 11 years
      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
    Sridhar Sarnobat about 11 years
    That didn't work for me. I did import time. I guess it's a version-related change of behavior.
  • Martín Coll
    Martín Coll about 11 years
    This worked great for me to return arrays of dictionaries, which aren't handled by Bottle
  • Martin Konecny
    Martin Konecny almost 11 years
    You shouldn't be returning a list of dictionaries, which is why bottle makes it so difficult (and Flask as well). See here: flask.pocoo.org/docs/security/#json-security
  • Henry Finucane
    Henry Finucane over 10 years
    This doesn't actually answer the question
  • Prof. Falken
    Prof. Falken almost 6 years
    Returning lists of dictionaries is good now. flask.pocoo.org/docs/security/#json-security