Front row: Program.cs and the HTTP messageBack row: the client and the running server (Kestrel, routing, JSON, the list)Tokens: code lines, request and response envelopes, a Todo and JSON text moving throughBack row, the running server: the client (curl), Kestrel, the routing table, System.Text.Json and the todos list in memory. Front row: the nine lines of Program.cs and the HTTP message on the wire. Lines 3 and 4 fill the routing table; POST /todos is matched, its JSON body becomes a Todo that is added to the list and answered with 201 Created; GET /todos returns the list as JSON with 200 OK; DELETE /todos gets 405 and /nope gets 404. The HTTP messages in the figure show only the headers that matter here. Real responses also carry Date, Server and Content-Length or Transfer-Encoding.

Web APIs

  1. A web API is a program that answers requests from other programs. A client sends an HTTP request: a method such as GET (read) or POST (create) and a path such as /todos. The API answers with a status code and data, usually JSON.

Build the app

Handle requests

What happens

// step 1/12 · web-api
client  →  request   GET /todos HTTP/1.1
            method   GET reads · POST creates
            path     /todos
server  →  response  a status code (200 OK) + data (JSON)

The HTTP messages in the figure show only the headers that matter here. Real responses also carry Date, Server and Content-Length or Transfer-Encoding. Kestrel can handle many requests at the same time on different threads, and List<T> is not safe for that. Real code uses a database, a lock or a thread-safe collection such as ConcurrentDictionary.