Flask routes
Flask maps URLs to functions. A route returns a value, and Flask builds the response. Returning a dict or list sends JSON.
from flask import Flask, request
app = Flask(__name__)
@app.get("/users/<int:user_id>")
def user(user_id):
return {"id": user_id}
@app.post("/users")
def add():
data = request.get_json(silent=True)
return {"ok": True}, 201- A route may return
(body, status). <int:x>only matches digits and passes anint, so other text gets a 404.abort(404)stops with that status.get_json(silent=True)returnsNonerather than an error for a body that is not JSON.
Testing without a server
app.test_client() drives the whole app through WSGI with no socket, which suits a phone well. In larger apps, a create_app() function builds a fresh app and fresh state for each test.
Your task
Finish create_app(). POST /items with JSON {"name": "tea"} stores an item with the next id from 1 and returns it with status 201. A body with no non-empty name, or a body that is not JSON, returns status 400 with {"error": ...}. GET /items/<int:item_id> returns the item or a 404.
The explanation opens here once every check passes.