1. 程式人生 > >json api

json api

blog req 全量 turn put quest evel append name

from flask import Flask, redirect, url_for, jsonify, request

app = Flask(__name__)
users = []
‘‘‘
RESTful api

Dr. Fielding
url 用資源來組織的 名詞

/GET /players 拿到所有玩家
/GET /player/id 訪問id的玩家的數據
/PUT /players 全量更新
/PATCH /players 部分更新
/DELETE /player/id 刪除一個玩家
/GET /player/id/level
‘‘‘


@app.route("/", methods=["GET"])
def index():
    return‘‘‘<form method=post action=‘/add‘>
    <input type=text name=author>
    <button>提交</button>
    </form>
    ‘‘‘


@app.route("/add", methods=["POST"])
def add():
    form = request.form
    users.append(dict(author=form.get("author", "")))
    return redirect(url_for(".index"))


@app.route("/json")
def json():
    return jsonify(users)


app.run()

  

json api