Skip to content
Commitpedia
Log in Join & edit

How an HTTP request works: methods, headers, and body

An HTTP request is a short text message a client sends to a server: a method and path, some headers, and sometimes a body. The server answers with a status, headers, and a body.

Not checked by a trusted editor yet Last edited Outdated?

Edit history of How an HTTP request works: methods, headers, and body: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
2 min

Every time a browser loads a page or an app calls an API, it sends an HTTP request and gets back an HTTP response. Both are made of the same three parts: a first line, a list of headers, and an optional body.

A request, line by line

Here is a request that creates a new item through a JSON API. The blank line separates the headers from the body.

POST /api/items?draft=true HTTP/1.1
Host: api.example.com
Content-Type: application/json
Accept: application/json
Authorization: Bearer YOUR_TOKEN_HERE
Content-Length: 34

{"name": "notebook", "price": 4.5}
  • Request line: the method (POST), the path plus optional query string (/api/items?draft=true), and the protocol version.
  • Headers: Name: value pairs that describe the request. Host says which site you want, Content-Type describes the body, Accept says what format you want back.
  • Body: the data you send. GET requests normally have none; POST, PUT, and PATCH usually do.

HTTP/2 and HTTP/3 send the same information in a compact binary form, but the model of method, path, headers, and body is identical, and tools such as browser DevTools show it to you in this readable shape.

The common methods

  • GET: read something. Should not change data on the server.
  • POST: create something or trigger an action.
  • PUT: replace a resource with the body you send.
  • PATCH: change part of a resource.
  • DELETE: remove a resource.
  • HEAD: like GET, but only the headers come back. OPTIONS: ask what is allowed; browsers use it for CORS preflight checks.

GET, HEAD, OPTIONS, PUT, and DELETE are idempotent: sending the same request twice has the same effect as sending it once. POST is not, which is why clients should be careful about retrying it automatically.

The response

HTTP/1.1 201 Created
Content-Type: application/json
Location: /api/items/42

{"id": 42, "name": "notebook", "price": 4.5}

The first line carries a three-digit status code: 2xx means success, 3xx a redirect, 4xx a problem with the request, and 5xx a problem on the server. Response headers describe the body, caching rules, cookies, and more.

What happens around the request

  1. The client looks up the server’s IP address with DNS.
  2. It opens a connection, and for https:// URLs it performs a TLS handshake so the traffic is encrypted and the server’s certificate is checked.
  3. It sends the request and waits for the response.
  4. The connection is usually kept open and reused for further requests to the same server.

Seeing it yourself

Open your browser’s DevTools, switch to the Network tab, and reload a page. Click any row to see the request headers, response headers, and body. From a terminal, curl -v https://example.com prints the lines it sends (marked >) and receives (marked <).

Two practical rules follow from all this. Never put secrets or personal data in the URL, because URLs end up in logs and browser history; send them in headers or the body over HTTPS. And always set Content-Type when you send a body, because many servers refuse or misread bodies without it.

Did this work for you?

No votes yet. Be the first.

If it did not work, the report form below tells the editors what went wrong.

See also

  • 0019 fetch with timeout, retries and proper error handling A reusable fetchJson helper for Node 20+ and browsers that adds a timeout, retries with backoff for temporary failures, and clear errors… Intermediate · Step by step · Any system Script Async code · Making requests 15 Oct 2026
  • 0083 Test a JSON API with curl A runnable bash script showing the curl commands you need most when testing an API: GET, POST JSON, custom headers, Bearer tokens,… Beginner · Step by step · Linux, macOS, Windows Script Authentication · Making requests 15 Oct 2026
  • 0084 Tiny JSON echo server for testing HTTP clients A standard-library Python server that echoes back the method, path, query, headers, and body of every request, and can return any status… Intermediate · Step by step · Any system Script Files & automation · Making requests 15 Oct 2026
  • 0085 fetch with timeout, retries, and exponential backoff A drop-in fetch wrapper that times out slow requests, retries network errors and 429/5xx responses with jittered exponential backoff, and honors Retry-After. Advanced · Step by step · Any system, Browser Script Async code · Making requests 15 Oct 2026
  • 0087 CORS explained: why the browser blocks your request CORS is the browser rule that decides whether JavaScript on one origin may read a response from another. The server opts in… Intermediate · Explains an idea · Browser Article Async code · Making requests 15 Oct 2026
  • 0088 Fix: Access to fetch at ‘…’ from origin ‘…’ has been blocked by CORS policy The browser refused to give your script a cross-origin response because the server did not send the right Access-Control-* headers. Fix it… Intermediate · Fixes an error · Browser Article Common errors · Making requests 15 Oct 2026

Repos in the wild

  • Requests: HTTP for humans psf/requests Requests is the classic Python library for calling web APIs and downloading pages, with a simple API for GET and POST requests, headers, JSON and sessions. GitHub
  • Axios axios/axios A promise-based HTTP client for browsers and Node.js with interceptors, automatic JSON handling, request cancellation and a consistent API in both environments. GitHub
  • curl and libcurl (curl/curl) curl/curl The source of curl, the command-line tool for transferring data with URLs, and libcurl, the C library behind it that many other programs use for HTTP. GitHub

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Commitpedia +3,112 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?