python版sinatraな軽量WEBフレームワークbottle - Bye Bye Mooreの続き。
bottleでJSONをゲットしたい場合……何も考えずに辞書型を放り込むと"Content-Type: application/json"で返してくれます。
実際のところ
文字と現在時刻を返したい場合、以下のようにします。
from bottle import get, run, template from time import strftime @get('/api/status') def api_status(): return {'status':'online', 'time': strftime('%m-%d_%H:%M:%S_%Z')} run(host='localhost', port=8080)
試しにcurlでチェック
$ curl http://localhost:8080/api/status {"status": "online", "time": "09-27_23:42:23_JST"}
オフにしたいとき
とはいえ、この挙動だと困る事もあるでしょう。
bottleの設定で変更できます。
bottle.default_app().autojson = false