{"record":{"id":"2615281d899bd889","repo":"tiangolo/fastapi","slug":"item-not-found-261528","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/app_testing/app_b_py310/main.py","lineNumber":25,"sourceCode":"    \"foo\": {\"id\": \"foo\", \"title\": \"Foo\", \"description\": \"There goes my hero\"},\n    \"bar\": {\"id\": \"bar\", \"title\": \"Bar\", \"description\": \"The bartenders\"},\n}\n\napp = FastAPI()\n\n\nclass Item(BaseModel):\n    id: str\n    title: str\n    description: str | None = None\n\n\n@app.get(\"/items/{item_id}\", response_model=Item)\nasync def read_main(item_id: str, x_token: str = Header()):\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item_id not in fake_db:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return fake_db[item_id]\n\n\n@app.post(\"/items/\")\nasync def create_item(item: Item, x_token: str = Header()) -> Item:\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item.id in fake_db:\n        raise HTTPException(status_code=409, detail=\"Item already exists\")\n    fake_db[item.id] = item.model_dump()\n    return item\n","sourceCodeStart":7,"sourceCodeEnd":37,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/app_testing/app_b_py310/main.py#L7-L37","documentation":"Same as error 1 in the legacy-style file: GET /items/{item_id} returns 404 when item_id is not a key in fake_db ('foo' or 'bar'). The store is in-memory and resets on restart.","triggerScenarios":"GET /items/<anything-but-foo-or-bar> with a valid token, or GETting an item created in a previous server process.","commonSituations":"Post-restart 404s, typos in item_id, or assuming seeded data includes more than foo/bar.","solutions":["GET /items/foo or /items/bar.","Create then read within the same process.","Back fake_db with persistent storage."],"exampleFix":"// before\nGET /items/missing\n// after\nGET /items/foo","handlingStrategy":"try-catch","validationCode":"import httpx\nKNOWN = {'foo', 'bar'}\nitem_id = 'foo'\nif item_id in KNOWN:\n    resp = httpx.get(f'http://localhost:8000/items/{item_id}', headers={'X-Token': 'coneofsilence'})","typeGuard":"def is_known_item(item_id: object) -> bool:\n    return isinstance(item_id, str) and item_id in {'foo', 'bar'}","tryCatchPattern":"try:\n    resp = httpx.get(url, headers=headers)\n    resp.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code == 404:\n        ...","preventionTips":["Do not rely on in-memory data after a restart.","Whitelist known ids before requesting.","Back the store with persistence for non-tutorial use."],"tags":["fastapi","not-found","in-memory-store","httpexception","app-testing"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}