The labs on this site need JavaScript to run. The same content is written out below.
Learn › Networking › HTTP
HTTP messages and status codes
This is what a request and a response look like as text
Why it exists — For a browser, an app, and a server made by different people to communicate, the shape of asking and answering must be fixed in advance. An HTTP message fixes the format of the first line, the headers, and the body, and the meaning of the methods and status codes, so any program can decide in the same way, without reading the body, whether the request succeeded, whether to go to a different address, or whether to try again a little later.
Where it's used today — The status code and headers of the ResponseEntity returned by a Spring controller become the response message as they are. When you design a REST API, you choose the status code that fits the result, such as 201 for creation, 400 for an input error, and 403 for no permission, and when a problem occurs, you find the cause by reading the messages that were actually exchanged in the browser developer tools Network tab or with curl -v.
In HTTP/1.1, the HTTP messages that a browser and a server exchange are text with a fixed shape (HTTP/2 and HTTP/3 send the same content split up in a binary format). A request has "what" (the address) and "how" (the method) on its first line, and a response has "how it went" (the status code) on its first line.
Checked against real execution · Sent the request text of all nine situations through a socket to a Spring Boot 4.1.1 server (Tomcat 11, Spring Security 7.1.1) that has the code on the screen, and checked against the raw responses
Concepts in this lab
- HTTP as a stateless protocol
- Why it exists — If the server had to remember the earlier conversation for each request, the resources needed to keep that memory would grow with the number of users, and a conversation could not continue once a connection was cut. HTTP is defined so that a request can be handled by looking at that request alone, so the server does not have to remember anything between requests.
- Where it's used today — Receiving web pages and images, a mobile app calling a server API, and a server calling another service's API are all done over HTTP. For features like chat, in which the server also has to send data first at any time, WebSocket is used together with it.
- HTTP is a communication protocol in which a client sends a request and a server responds. Stateless means that the server does not remember the content of earlier requests, so each request must carry all the information needed to process it. Because the server keeps no state, any server can take a request, which makes it easy to add servers. Information that must carry over, such as a login state, is kept with cookies, sessions, or tokens.
- HTTP methods
- The part of an HTTP request that indicates the action the request wants to perform on a resource. GET means read, POST means create or a processing request, PUT means replace the whole resource, PATCH means modify part of it, and DELETE means delete. Use each one according to its meaning so that HTTP behavior such as caching and retries works as expected.
- HTTP status codes
- Why it exists — If results were reported only as text in the body, each server would write them differently, and browsers, proxies, and monitoring tools could not tell whether a request succeeded. A status code reports the result as a fixed number, so these tools can do caching, retries, and error counting without being adjusted for each server.
- Where it's used today — It is used to branch handling by the kind of error, such as a frontend that sends the user to the login screen on a 401 and to a "page not found" notice on a 404. In operations, you watch the ratio of 5xx responses and, if it suddenly rises, treat it as an outage and send an alert.
- A three-digit number that shows the result of the server's handling of a request. 2xx means success, 3xx means a move to another location (redirection), 4xx means a problem with the client's request, and 5xx means an error on the server side. Common ones are 200 (success), 201 (created), 400 (bad request), 401 (authentication required), 403 (no permission), 404 (not found), and 500 (server error).
- REST
- Why it exists — If every API names actions in its own way in the address, such as /getUser and /deleteUserById, people who use the APIs have to learn new rules for each API. REST makes the address point only to a resource and express the action with an HTTP method, so even an API you see for the first time can be read with the same rules.
- Where it's used today — It is commonly followed when designing APIs through which web screens or mobile apps read and change a server's data, and APIs that are open to the public outside a company. Sometimes GraphQL is used instead to pick only the fields each screen needs, and gRPC to call between servers quickly.
- An architectural style in which resources on the web are identified by URIs, actions on those resources are expressed with HTTP methods, and representations of the resources (such as JSON) are exchanged. Its characteristics are that it is stateless, meaning the server does not store the client's state, and that it has a uniform interface. In practice, an HTTP API designed according to these principles is called a REST API.
- URI and URL
- A URI is a string that identifies a resource, and a URL is a URI that identifies the resource by its location (how to access it and its address). Most addresses used on the web are URLs, and every URL is a URI.
Try these
- Default settings — A successful read. The response comes with 200 and a body.
- Create a new order — The response is 201, and it tells you the address of the newly created order.
- Invalid request — 400. The sender has to fix it.
- Not logged in — Without logging in, you get a 401. For the 403 you get when you are logged in but have no permission, see the situation "Get another user's order".
- Order that does not exist — 404. There is nothing at the address.
- Server error — 500. The server has to fix it, not the sender.
Running with the default settings
- This is the first line of the request, the start line. It holds the method GET, the target path /orders/1, and the protocol version, in that order. The method says "what to do", and the path says "what it is about".
- The next lines are headers. Each one is written as "name: value" and carries extra information about the request. Host says which site the request is for, Authorization says who is sending it.
- An empty line marks the end of the headers. This request has no data to send, so it has no body. The server has to be able to handle the request by looking at this one request alone, because it does not remember the requests sent before it (it is stateless).
- This is the response from the server. The first line (the start line) holds the protocol version and the status code 200. The name that the standard gives this code is OK. This server (the embedded Tomcat of Spring Boot) does not write the name on the start line and sends only the number. The first digit of the three-digit number is the broad category: 2xx Successful.
- The server finds the requested order and returns it in the body.
Result of this run
- The first digit of the status code alone tells you how to respond. With 4xx, the request usually has to be fixed (a few, such as 408 and 429, can succeed if you send the same request again after a short while). 5xx is a problem on the server side, so you can try again after a short while. Monitoring also counts 4xx and 5xx separately.
- An HTTP/1.1 message has four parts: the start line, the headers, an empty line, and the body. The start line of a request holds the method and the path, and the start line of a response holds the status code.
Code
// Only the controller method is shown (class and field declarations, imports, and the example types' declarations are omitted)
@GetMapping("/orders/{id}")
OrderDto get(@PathVariable long id) {
return OrderDto.of(orderService.find(id)); // 200 OK + JSON body
}
Assumptions of this model
All nine situations were checked by sending the request text on the screen as it is, through a socket, to a real server that has the code on the screen (the controller methods and the security configuration), and confirming that the start line, the headers shown on the screen, and the body of the text that came back match the model. The server is Spring Boot 4.1.1 (embedded Tomcat 11.0.24, Spring Security 7.1.1, JDK 21). In place of "Bearer eyJ…" in the request, a real JWT signed with a test key (member 7) was used, Content-Length was added to requests that have a body, and Connection: close was added to every request. Only the main headers are kept on the screen. The real responses have more headers, such as Date, Content-Length or Transfer-Encoding, and the Cache-Control and X-Content-Type-Options headers that Spring Security adds, and the 401 response also had a JSESSIONID cookie. An ellipsis (…) marks where the rest of a value is omitted, and the timestamp value in an error body changes with every response, so it is written as "(time of the response)". Tomcat does not write the name of the status code (OK, Not Found, and so on) on the start line and sends only the number, as in "HTTP/1.1 200 ". The names in the result table are the names given by the HTTP standard (RFC 9110, section 15), and some servers, such as nginx, send the name along with the number. The bodies of the 400 and 500 responses are the default error response of Spring Boot, and the headers of the 401 and 403 responses are produced by the Bearer token handling of Spring Security, so they differ by version and configuration. Order, OrderDto, OrderRequest, OrderNotFoundException, orderService, and orderRepository are made up for this example, and the other annotations and classes belong to Spring and Bean Validation. Which status code to use in each situation follows the definitions in the HTTP standard and the conventions widely used in REST APIs. Some designs return 404 instead of 403 for a request about another user's resource, to hide that the resource exists.
Previous lab: Flow control and congestion control · Next lab: HTTP/1.1, 2, 3