StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 3 · Request Body and Pydantic Models

Item Creation Model

easy0%0 of 1 solved

Problem statement

The create endpoint takes no body, so nothing can be added to the inventory. Declare the body model and accept it, so a well formed item is converted, defaulted and stored, and a malformed one is refused with a report naming the field that broke.

Sample Data

The inventory in app/store.py starts with three items, holding the ids 1_ to _3. Here is one item record:

{
  "id": 1,
  "name": "Aurora Desk Lamp",
  "price": 42.5,
  "description": "Warm bedside light",
  "in_stock": true
}

API Endpoints to Implement

ItemCreate

The body model, in app/models/item.py, holding four fields:

  • name, text, required.
  • price, a number, required.
  • description, text or null, optional, default null.
  • in_stock, a boolean, optional, default true.

A key the model does not declare is ignored rather than stored, and it cannot stand in for a declared one: a body carrying cost but no price is still refused for a missing price.

POST /items

Accepts an item and stores it.

  • Take the body as an ItemCreate and hand it to store_item in app/services/inventory.py, which gives the item the next id after the largest stored and appends it.
  • Response code: 200. The body is the stored record, holding id, name, price, description and in_stock and nothing else.
  • A value readable as its declared type is converted to it. A price of 19.99 sent as text, or of 120 sent whole, is stored as a number. in_stock reads true, 1_, _yes, y_, _t, on and TRUE as true and false, 0_, _no, n_, _f, off and Off as false, sent as a boolean, a number or text.
  • A refused body is not stored.

Rejected requests

A request is answered with 422 when the body is absent, is not an object, or holds a field that is missing or of the wrong type. The body holds one key detail, a list with one entry per broken rule, each carrying loc, ["body"] for the body itself or ["body", "field name"] for one field, input, the value received, which for a missing field is the whole body, and a type with its fixed msg:

  • missing, Field required.
  • model_attributes_type, Input should be a valid dictionary or object to extract fields from.
  • string_type, Input should be a valid string.
  • float_parsing, Input should be a valid number, unable to parse string as a number, for a price sent as text no number reads from.
  • float_type, Input should be a valid number, for a price that is no kind of number at all, null included.
  • bool_parsing, Input should be a valid boolean, unable to interpret input.

The fields must be declared on the model rather than read out of the request inside the handler, since the service publishes the body shape at GET /openapi.json, naming the four fields, requiring name and price, and reporting true as the default of in_stock.

Sample Requests & Responses

POST /items

Request:

{
  "name": "Kelp Wall Art",
  "price": 47.0
}

Response (200 OK):

{
  "id": 4,
  "name": "Kelp Wall Art",
  "price": 47.0,
  "description": null,
  "in_stock": true
}

POST /items

Request:

{}

Response (422 Unprocessable Entity):

{
  "detail": [
    {
      "type": "missing",
      "loc": ["body", "name"],
      "msg": "Field required",
      "input": {}
    },
    {
      "type": "missing",
      "loc": ["body", "price"],
      "msg": "Field required",
      "input": {}
    }
  ]
}

More in Request Body and Pydantic Models

See the module
Field Constrained OrderThis problem covers putting limits on each value an order carries, so a quantity, a price or a note outside what the business allows is refused before the order is ever placed.—No data yeteasyNested Shipment StructureThis problem covers describing data that has parts inside parts, an address and a list of parcels within one shipment, so a mistake anywhere is reported at the exact place it happened.—No data yeteasyBooking Request PayloadThis problem covers taking a booking that arrives split across the address, the query and a nested body, checking every part of it, and turning down anything that does not fit before the trip is even looked up.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.