StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 5 · Form Data, Files & Cookies/Headers

Header And Cookie Inputs

easy—No data yet

Problem statement

Build an update check that reads the caller's version from a request header and their release channel from a cookie, reports how their build compares with the latest one, and pins the channel back for next time.

Sample Data

app/store.py publishes a latest version per channel: stable is 3.8.2, beta is 4.0.0 and nightly is 4.1.0.dev5.

API Endpoints to Implement

GET /updates/check

  • X-Client-Version arrives as a request header, text, required. A header name is matched whatever its casing, so x-client-version and X-CLIENT-VERSION are read the same way.
  • channel arrives as a cookie, text, optional, default stable.
  • Hand the channel to latest_version in app/services/updates.py, which answers with that channel's latest version, falling back to the stable one for a channel it does not publish. The channel is still reported as it was sent.
  • Response code: 200. The body holds exactly client_version as it was sent, channel, latest, and up_to_date, true only when the two versions are the same.
  • The response also names the latest version in a header of its own, X-Latest-Version.
  • The response pins the channel with exactly one cookie, channel=<the channel>; HttpOnly; Max-Age=2592000; Path=/; SameSite=lax.

Rejected requests

A request without the version header is answered with 422 and one detail entry carrying loc of ["header", "x-client-version"], an input of null, and the type and msg reported for the rule.

The header and the cookie must be declared on the handler rather than read out of the request, since the service publishes the contract at GET /openapi.json, where the operation carries exactly one header parameter, x-client-version, required text, exactly one cookie parameter, channel, optional text defaulting to stable, and declares both 200 and 422.

Sample Requests & Responses

GET /updates/check, sending an X-Client-Version of 3.8.2 and no cookie

Response (200 OK):

{
  "client_version": "3.8.2",
  "channel": "stable",
  "latest": "3.8.2",
  "up_to_date": true
}

GET /updates/check, sending an X-Client-Version of 3.5.0 and a channel cookie of beta

Response (200 OK):

{
  "client_version": "3.5.0",
  "channel": "beta",
  "latest": "4.0.0",
  "up_to_date": false
}

GET /updates/check, sending a channel cookie of ancient, which is not published

Response (200 OK):

{
  "client_version": "3.8.2",
  "channel": "ancient",
  "latest": "3.8.2",
  "up_to_date": true
}

GET /updates/check, sending no version header

Response (422 Unprocessable Entity):

{
  "detail": [
    {
      "type": "missing",
      "loc": ["header", "x-client-version"],
      "msg": "Field required",
      "input": null
    }
  ]
}

More in Form Data, Files & Cookies/Headers

See the module
Login Form FieldsThis problem covers signing a member in from the values a browser form submits, so a matching pair is greeted by name and a wrong one is turned away without saying which half was wrong.—No data yeteasyValidated File UploadThis problem covers taking a file someone uploads together with a note about it, turning away anything of the wrong kind or over the size allowed, and reporting back what arrived.—No data yeteasyDocument Portal IntakeThis problem covers taking a submission that arrives in several pieces at once, a form, files, a header and a cookie, routing it to the right desk, and refusing the pieces that break a rule.—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.