Flask JSON API



examples/flask/20/app.py
from flask import Flask, jsonify
app = Flask(__name__)

@app.route("/")
def main():
    return '''
Main <a href="/api/info">info</a>
'''

@app.route("/api/info")
def api_info():
    info = {
       "ip" : "127.0.0.1",
       "hostname" : "everest",
       "description" : "Main server",
       "load" : [ 3.21, 7, 14 ]
    }
    return jsonify(info)

$ curl -I http://localhost:5000/api/info
HTTP/1.0 200 OK
Content-Type: application/json


examples/flask/20/test_app.py
import app


def test_app():
    web = app.app.test_client()

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert b'Main <a href="/api/info">info</a>' in rv.data

    rv = web.get('/api/info')
    assert rv.status == '200 OK'
    #print(rv.data) # the raw json data
    assert rv.headers['Content-Type'] == 'application/json'
    resp = rv.json
    assert resp == {
       "ip" : "127.0.0.1",
       "hostname" : "everest",
       "description" : "Main server",
       "load" : [ 3.21, 7, 14 ]
    }