APIs with FastAPI · Chapter 3 of 26

Request bodies

By the end of this chapter you can take a JSON body in a POST, describe it with a Pydantic model, put rules on each field, nest one model inside another, write a rule across two fields, read what the client gets for bad input, and say why the server checks what the client's app already checked. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First API
  • 4 tasks
  • FastAPI
  • In the all labs plan

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.

Why you need this

Merchants want to send DakiyaGo things now, not only ask: a parcel's weight and destination for a price, a whole booking with two addresses. A booking does not fit in a path or a query string; it is a JSON object with objects inside it. And Sunita knows what arrives today on WhatsApp: a phone number with nine digits, a pincode with five, "COD 500" on a parcel the customer already paid for, a weight of "1.2kg approx". Every one of those costs a rider a wasted trip. The API is the one place every booking passes through, so it is where they must be caught.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    quote.py: a price before the customer pays

    Make an app with one route: POST /quote takes a JSON body with from_pincode, to_pincode and weight_grams, and answers {"zone": ..., "slabs": ..., "price": ..., "days": ...}. The zone is the to_pincode's zone in pincodes.json; a slab is 500 g or any part of it (500 g is one slab, 501 g two); the price is the zone's first_slab plus each_extra_slab for every slab after the first; days is the zone's days in rate_card.json.

    Refuse with 422: a pincode that is not six digits, a weight that is not a whole number from 1 to 30000, a missing field, and a to_pincode DakiyaGo does not serve now (not in the file, or not active).

  2. Task 2

    booking.py: a booking with two addresses

    Make an app with one route: POST /bookings takes merchant_id (M and two digits), pickup and drop (each an address: name 1 to 60 characters, phone exactly 10 digits, line1 1 to 120 characters, pincode exactly 6 digits), weight_grams (a whole number from 1 to 30000) and cod_amount (a whole number from 0 to 50000, 0 when not sent).

    Answer the booking back as FastAPI read it, cod_amount filled in. Anything that breaks a rule answers 422, and the problem points at the exact field, such as body, drop, phone.

  3. Task 3

    cod.py: cash on delivery, only where it works

    Make an app with one route: POST /bookings takes to_pincode (6 digits), weight_grams (1 to 30000), payment (prepaid or cod) and cod_amount (0 to 50000, 0 when not sent), and answers the booking back as FastAPI read it.

    The rules across fields: a prepaid booking has cod_amount 0; a cod booking has cod_amount above 0 and a to_pincode that is in pincodes.json with cod true. A booking that breaks any rule answers 422 with FastAPI's usual list of problems ({"detail": [...]}).

  4. Task 4 · interview question

    Interview: pickup.py, only what we offer

    Make an app with one route: POST /pickups takes merchant_id (M and two digits), parcels (a whole number from 1 to 50) and slot (morning or evening), and answers the pickup back as FastAPI read it.

    A value out of range answers 422, and so does a body with any field the model does not have (such as price or priority): refused, with the problem pointing at that field. The interviewer will ask: the merchant's app already checks its form, so why validate on the server too?

What you will learn

  • POST with a JSON body, and what is on the wire.
  • A Pydantic model that describes the body, and what your function gets.
  • Field rules (Field, Literal), and the 422 a client gets for each broken one.
  • Nested models, and validators across fields.
  • Extra fields, and why the server validates anyway.

More in First API

Build "Request bodies" yourself

DakiyaGo, a Jaipur courier startup, takes bookings on WhatsApp. Merchants want an API, and you build it. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with FastAPI are free. This one comes with the all labs plan.