Notes API in Flask
Build a small notes service in Flask. Keep the Flask object named app; notes live in memory (a dict is enough).
POST /notes with a JSON body {"text": "..."} creates a note and returns status 201 with {"id": 1, "text": "..."}. Ids start at 1 and go up by one for each note created.
If the body isn't JSON, or text is missing, isn't a string, or is empty after trimming spaces, return status 400 with {"error": "text is required"}. A rejected request doesn't use up an id.
GET /notes/<id> returns status 200 with the note, or 404 with {"error": "not found"}.
The checker doesn't start a real server: it sends requests through Flask's test client, the same way you'd test an API in a real project. Each test starts from a fresh app.
Examples
- # Create a note POST /notes {"text": "Buy milk"} → 201 {"id": 1, "text": "Buy milk"}
- # Read it back POST /notes {"text": "Buy milk"} → 201 GET /notes/1 → 200 {"id": 1, "text": "Buy milk"}
- # Unknown id GET /notes/42 → 404 {"error": "not found"}
- # Empty text POST /notes {"text": ""} → 400 {"error": "text is required"}
Before you start: read the basics
- Flask Quickstart ↗Routes, the request object, returning JSON and status codes.
- Testing Flask Applications ↗How the test client sends requests: this is how your code is checked.
- HTTP response status codes (MDN) ↗What 200, 201, 400 and 404 mean.
Show hint
request.get_json(silent=True) returns None instead of raising an error when the body isn't JSON. A view can return a pair: return jsonify(error="not found"), 404.