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>
JavaScript Object Notation (JSON) is a standardized format commonly used to transfer data as text that can be sent over a network. It's used by lots of APIs and Databases, and it's easy for both humans and machines to read. JSON represents objects as name/value pairs, just like a Python dictionary.
Run this script or paste it into a Python console, then point your browser to http://localhost:8080/hello/world. That's it. Install the latest stable release with pip install bottle or download bottle.py (unstable) into your project directory. There are no hard [1] dependencies other than the Python standard library.
Flask-JSON is a simple extension that adds better JSON support to Flask application. It helps to handle JSON-based requests and provides the following features: json_response() and @as_json to generate JSON responses.
Flask is one of the most widely used python micro-frameworks to design a REST API. In this article, we are going to learn how to create a simple REST API that returns a simple JSON object, with the help of a flask.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With