Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottle and Json

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> 
like image 724
arinte Avatar asked Aug 17 '10 20:08

arinte


People also ask

What is JSON () in Python?

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.

How do you use a bottle in Python?

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.

Does flask use JSON?

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.

Can flask return JSON?

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.


1 Answers

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

like image 106
Andrew Avatar answered Oct 06 '22 07:10

Andrew