HTTP fundamentals for API authors
Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.
What you will be able to do
#- Map operations to HTTP methods and explain which are safe and idempotent.
- Choose an accurate status code for success, client error and server error cases.
- Read and set the headers that carry content type, caching and authentication.
Overview
#HTTP is a stateless request and response protocol. The client sends a method, a target, headers and an optional body; the server replies with a status code, headers and an optional body.
Methods and semantics
GETreads a representation and must be safe (no side effects) and idempotent.POSTcreates a resource or triggers an action; not idempotent.PUTreplaces a resource; idempotent.PATCHpartially updates; not guaranteed idempotent.DELETEremoves a resource; idempotent.
Idempotent means repeating the request has the same effect as sending it once. That matters for retries after a network timeout.
Status codes
200OK,201Created with aLocationheader,204No Content.400malformed request,401not authenticated,403authenticated but not allowed,404not found,409conflict,422semantically invalid.500unexpected server fault,503temporarily unavailable.
POST /articles HTTP/1.1
Content-Type: application/json
{ "title": "Indexes" }
HTTP/1.1 201 Created
Location: /articles/42
Content-Type: application/json
Headers that matter
Content-Type tells the parser how to read the body. Accept negotiates the response format. Authorization carries credentials. Cache-Control and ETag drive caching. Retry-After accompanies 429 and 503.
Common pitfalls
- Returning
200with an error object instead of a correct status code. - Using
GETfor an operation that mutates state. - Ignoring
Content-Typeand assuming the body is JSON.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A client sends a valid body to create a resource, but the resource already exists with that unique key. Which status code is most accurate?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.httpPOST /users { "email": "a@example.com" } // email already registeredExercise 2Explain
Explain why a retried POST can create two rows while a retried PUT cannot, and how an idempotency key fixes the POST case.
starter.text// Describe the retry behavior of POST versus PUT, // then describe what an Idempotency-Key header changes.Reveal the reference answer
reference.textPUT sets a resource to a known state, so sending it twice leaves the same state. POST appends a new resource each time, so a retry after a lost response creates a duplicate. An Idempotency-Key header lets the server remember the first result for that key and return it again instead of creating a second resource.Authored expected output: An explanation that identifies PUT as idempotent, POST as non-idempotent, and describes server-side key storage that replays the first response.
Idempotency keys turn an unsafe operation into a safely retryable one by deduplicating on a client-generated key.
Common misconceptions
#- Any non-2xx code is a server bug: 4xx codes describe client mistakes and are correct responses.
- PUT and PATCH are interchangeable: PUT replaces the whole representation, PATCH applies a partial change.
Follow-up practice
#- backend-databases-node-http-server
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.