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

Login Form Fields

easy—No data yet

Problem statement

Build a sign-in endpoint that reads a username and password from a submitted form, answers with the member behind them, and turns away a pair that does not match.

Sample Data

The register in app/store.py holds three members. Here is one member record:

{
  "username": "avery",
  "password": "trellis-9",
  "display_name": "Avery Quinn"
}

API Endpoints to Implement

POST /login

  • username and password arrive as form fields, text, both required. They are submitted as a form, never as JSON, so a JSON body supplies neither.
  • Hand both to authenticate in app/services/members.py, which answers with the matching member or with nothing.
  • Response code: 200, the body holding username and display_name.
  • A wrong password and an unknown username are both answered with 401 and {"detail": "Invalid username or password"}, the same message either way.

Rejected requests

A missing field is answered with 422 and one detail entry per absent field, carrying loc of ["body", "field name"], an input of null, and the type and msg reported for the rule. A JSON body reports both fields absent.

The fields 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 request body is required, is offered as a form and not as JSON, and points at a named schema of its own listing both fields as required text.

Sample Requests & Responses

POST /login

Request:

username=avery&password=trellis-9

Response (200 OK):

{
  "username": "avery",
  "display_name": "Avery Quinn"
}

POST /login

Request:

username=avery&password=quartz-42

Response (401 Unauthorized):

{
  "detail": "Invalid username or password"
}

POST /login, the same credentials sent as a JSON body

Response (422 Unprocessable Entity):

{
  "detail": [
    {
      "type": "missing",
      "loc": ["body", "username"],
      "msg": "Field required",
      "input": null
    },
    {
      "type": "missing",
      "loc": ["body", "password"],
      "msg": "Field required",
      "input": null
    }
  ]
}

More in Form Data, Files & Cookies/Headers

See the module
Validated 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 yeteasyHeader And Cookie InputsThis problem covers reading the small pieces of context a client sends alongside a request, its version and its chosen release channel, and answering with both a body and headers of its own.—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.