Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 79 ยท Application Layer: Names and Web

HTTP Fundamentals

Learn how HTTP works: URLs, the request and response format, methods and idempotency, status codes, headers, statelessness and content negotiation, with live Node.js demos.

The Hypertext Transfer Protocol (HTTP) is the language of the web. Your browser speaks it to load pages, your phone app speaks it to fetch data, and countless programs speak it to talk to each other through APIs. It is a client-server request-response protocol: the client sends a request, the server sends back a response, and that is the whole conversation.

HTTP began as a protocol of readable text lines, and the core ideas have stayed the same through HTTP/1.1, HTTP/2 and HTTP/3. In this lesson you learn what a URL contains, what the bytes of a request and response look like, what methods and status codes mean, and how headers carry the details. Then you run a real HTTP server and client on your own loopback address.

URLs

A URL says where a resource is and how to get it. It has several parts:

TEXT
https://[email protected]:8443/docs/intro.html?lang=en&page=2#install
\___/   \__/ \_____________/ \__/\______________/ \___________/ \_____/
scheme  user      host       port     path           query      fragment
  • The scheme names the protocol (http, https). The default ports are 80 for http and 443 for https, so you rarely see them.
  • The host is a name (resolved with DNS) or an IP address.
  • The path names the resource on that server.
  • The query carries parameters as key=value pairs.
  • The fragment (after #) is used only by the browser to jump inside the page. It is never sent to the server.

Characters that are not allowed or that have a special meaning are written as percent-encoding: a space becomes %20, and รฉ becomes %C3%A9.

Node.js Taking a URL apart

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice the last two lines before the encoding example. The client connects to the host using TCP (see ports), and sends only the path and query as the request target. The host name is repeated in a Host header, which lets one server and one IP address serve many websites.

The request

An HTTP/1.1 request is plain text in three parts: a request line, headers, and an optional body. Lines end with CRLF (carriage return and line feed), and a blank line marks the end of the headers.

TEXT
GET /hello?name=Ada HTTP/1.1
Host: www.example.com
User-Agent: by-hand/1.0
Accept: text/plain

The request line has the method, the target and the protocol version. Then come Name: value headers. A request that sends data, such as a form submission, adds a body after the blank line and tells the server how long it is (Content-Length) and what it contains (Content-Type).

The response

The response starts with a status line: the version, a three-digit status code and a short reason phrase. Headers follow, then a blank line, then the body.

TEXT
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Content-Length: 40

You asked for /hello?name=Ada using GET

Nothing stops you from writing these bytes yourself. The program below starts a small HTTP server on loopback, connects to it with a raw TCP socket (not an HTTP library), sends a hand-written request, and prints exactly what comes back. <CRLF> marks every line ending so you can see the format.

Node.js HTTP by hand over a raw TCP socket

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

There is no magic: a request is some lines of text, a response is some lines of text, and the only trick is knowing where the body starts (after the blank line) and where it ends. Here Content-Length: 40 says the body is 40 bytes long. Other ways to mark the end are Transfer-Encoding: chunked (sizes in front of each piece, used when the length is unknown in advance) and closing the connection. This is the same framing problem every protocol over TCP must solve.

Methods

The method tells the server what the client wants to do with the resource.

Method Meaning Safe Idempotent Has body
GET Read the resource yes yes no
HEAD Same as GET, but only the headers yes yes no
POST Submit data, create something, trigger an action no no yes
PUT Replace the resource with the body no yes yes
PATCH Change part of the resource no not necessarily yes
DELETE Remove the resource no yes rarely
OPTIONS Ask which methods and features are allowed yes yes no

Safe means the request does not change anything on the server, so browsers, crawlers and caches may issue it freely. Idempotent means that doing it many times has the same effect as doing it once: deleting the same note twice leaves the same state as deleting it once. That matters for retries after a network failure. A lost PUT can be resent safely; a lost POST that "creates an order" might create two.

Never use GET for actions that change data, such as GET /delete?id=5. Link prefetchers, crawlers and browser extensions follow GET links without asking.

Status codes

The first digit of the status code gives the class:

Class Meaning Common codes
1xx Informational Keep going 100 Continue, 101 Switching Protocols (used by WebSockets)
2xx Success It worked 200 OK, 201 Created, 204 No Content
3xx Redirection Look somewhere else or use your cache 301 Moved Permanently, 302 Found, 304 Not Modified, 307/308 Temporary/Permanent Redirect
4xx Client error You made a mistake 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 405 Method Not Allowed, 409 Conflict, 422 Unprocessable Content, 429 Too Many Requests
5xx Server error The server failed 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout

Two of these are often confused. 401 really means "not authenticated" (log in first), while 403 means "I know who you are, and you are not allowed". And 502, 503 and 504 usually come from a proxy or load balancer in front of an application: the proxy is fine but the server behind it is broken, overloaded or too slow.

The next program is a small REST-style notes service with real routes. It calls itself using fetch and prints one line per call, so you see methods, paths and status codes together with the headers that matter (Location, Allow, Content-Type).

Node.js Methods and status codes on a loopback API

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Study how the server picks its answers:

  • A successful POST returns 201 Created with a Location header that names the new resource.
  • Bad JSON would be 400; valid JSON with a missing field is 422, because the request was understood but cannot be processed.
  • DELETE returns 204 No Content: success, nothing to send back.
  • Using PUT on a path that only supports GET and POST gives 405 and an Allow header listing the valid methods.
  • /old-notes answers 301 with a Location. We told fetch not to follow redirects (redirect: 'manual') so you can see the redirect itself; browsers follow it automatically.

Headers

Headers are metadata about the message, in both directions. Here are the ones you will meet constantly:

Header Direction Purpose
Host request Which site the request is for (required in HTTP/1.1)
User-Agent request Which client software is making the request
Accept request Which content types the client can handle
Accept-Language, Accept-Encoding request Preferred languages and compression (gzip, br)
Authorization request Credentials, such as a bearer token
Cookie / Set-Cookie request / response Client state (see cookies)
Content-Type both Media type of the body, e.g. application/json
Content-Length both Body size in bytes
Location response Where a new or moved resource lives
Cache-Control, ETag response Caching rules (see caching)
Server, Date response Server software and time

Header names are case-insensitive. You can invent your own, traditionally with an X- prefix, but standard headers are preferred.

Statelessness

Each HTTP request is independent: the server does not have to remember anything from the previous one. That keeps servers simple and makes it easy to add more of them behind a load balancer, since any server can answer any request. But sites do need memory: who is logged in, what is in the cart. The solution is to put the state into the requests themselves, usually as a small identifier in a cookie or an Authorization header that the client sends every time; the server looks up the matching data. The caching and cookies lesson covers this in detail.

Content negotiation

One URL can offer several representations of a resource: JSON or HTML, English or German, gzip-compressed or not. The client lists its preferences with Accept headers and the server picks the best match and says what it chose in Content-Type, Content-Language or Content-Encoding. You can see it in the second call of the notes program: the same GET /notes returns JSON by default and plain text when the request says Accept: text/plain. Preferences can carry weights, as in Accept: text/html, application/json;q=0.8, */*;q=0.1, where q ranges from 0 to 1. If the server varies its answer based on a header, it should say so with a Vary header so caches do not mix the versions up.

Seeing real HTTP

On a real machine, curl -v shows the same exchange. This is read-only sample output of a request to a site you do not control, so it can differ from run to run:

TEXT
$ curl -v https://www.example.com/
> GET / HTTP/2
> Host: www.example.com
> user-agent: curl/8.5.0
> accept: */*
>
< HTTP/2 200
< content-type: text/html; charset=UTF-8
< cache-control: max-age=604800
< content-length: 1256
<
<!doctype html> ...

Lines starting with > are sent and lines with < are received. Over HTTP/2 the headers are binary and lowercased, but they carry the same information, as you will see in HTTP/1.1, HTTP/2 and HTTP/3. More tools are in essential network tools.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What marks the end of the headers in an HTTP/1.1 message?

    Headers are lines ending in CRLF, and an empty line signals that the header section is over. The body, if any, follows.
  2. 2. A client resends a request after a timeout and a duplicate order is created. Which method was most likely used?

    POST is neither safe nor idempotent, so repeating it can repeat its effect. GET, PUT and DELETE are defined to be idempotent.
  3. 3. Which status code should a server return after successfully creating a new resource?

    201 tells the client a new resource exists, and the Location header gives its URL.
  4. 4. A user is logged in but tries to open a page for administrators only. Which status fits best?

    403 means the server understood who you are and refuses access. 401 means authentication is missing or invalid.
  5. 5. Why is it said that HTTP is stateless?

    The protocol itself keeps no memory between requests. Applications add state with cookies, tokens or similar identifiers sent in each request.

Summary

  • A URL names the scheme, host, port, path, query and fragment. The fragment never leaves the browser, and special characters are percent-encoded.
  • An HTTP/1.1 message is text: a start line, headers, a blank line and an optional body, with the body length given by Content-Length or chunked encoding.
  • Methods express intent. GET and HEAD are safe; GET, PUT and DELETE are idempotent; POST is neither.
  • Status codes are grouped by their first digit: 2xx success, 3xx redirection, 4xx client error, 5xx server error. 401 is "who are you?", 403 is "not allowed".
  • Headers carry metadata, drive content negotiation and caching, and are case-insensitive.
  • HTTP is stateless; cookies and tokens add state on top.

Next, see how HTTP kept getting faster without changing these ideas in HTTP/1.1, HTTP/2 and HTTP/3.