Skip to article content
FrontendXLesson 02 / 24

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.

beginner · about 45 minutes · 2 exercises

  • Predict
  • Explain

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

  • GET reads a representation and must be safe (no side effects) and idempotent.
  • POST creates a resource or triggers an action; not idempotent.
  • PUT replaces a resource; idempotent.
  • PATCH partially updates; not guaranteed idempotent.
  • DELETE removes 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

  • 200 OK, 201 Created with a Location header, 204 No Content.
  • 400 malformed request, 401 not authenticated, 403 authenticated but not allowed, 404 not found, 409 conflict, 422 semantically invalid.
  • 500 unexpected server fault, 503 temporarily unavailable.
snippet.http
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 200 with an error object instead of a correct status code.
  • Using GET for an operation that mutates state.
  • Ignoring Content-Type and 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.

  1. 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.http
    POST /users
    { "email": "a@example.com" }
    // email already registered
    Choose the best answer
  2. Exercise 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.text
    PUT 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

Related resources

#

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.

© 2026 FrontendX. All rights reserved.