HTTP Request and Response

HTTP enables clients and servers to communicate through requests and responses. A request specifies an operation with a method, target, headers, and optionally a body; the response provides a status code, headers, and optionally data.

An HTTP request is sent by a client to a server to ask for an operation on a resource. Its main parts are the request method, request target, headers, and optional body. Common methods include `GET`, `POST`, `PUT`, `PATCH`, and `DELETE`.

An HTTP response is the server's reply. It contains a status code, response headers, and optionally a body containing data such as JSON, HTML, or plain text. Status codes are grouped into classes: `2xx` indicates success, `3xx` redirection, `4xx` client-side errors, and `5xx` server-side errors.

Headers carry metadata about the request or response. Examples include `Content-Type`, `Accept`, `Authorization`, `Cache-Control`, and `Content-Length`. The body carries the actual payload when one is needed, such as JSON submitted to an API.

HTTP is commonly used with REST-style APIs in Java backend applications. A Java server framework such as Spring can map an HTTP request to controller code, process it, and serialize the result into an HTTP response.

Key points

Interview questions

Why are HTTP methods important in API design?

HTTP methods communicate the intended operation and give clients and intermediaries a consistent protocol-level meaning. For example, `GET` is commonly used to retrieve data, while `POST` commonly submits data for processing or creates a subordinate resource. `PUT`, `PATCH`, and `DELETE` are commonly associated with replacement, partial modification, and deletion respectively. Correct method selection also affects properties such as safety and idempotency.

What are the main parts of an HTTP request and response?

An HTTP request has a method, target, headers, and an optional body. The method indicates the intended operation, while headers provide metadata and the body carries optional application data. A response contains a status code, headers, and an optional body. The status code communicates the outcome of processing the request.

What is the purpose of HTTP headers?

Headers carry metadata associated with an HTTP request or response. For example, `Content-Type` describes the media type of a body, while `Authorization` can carry credentials or a token. Headers allow the client and server to communicate protocol and application information without putting that metadata into the body.

What do HTTP status code classes mean?

Status codes are grouped by their first digit. `2xx` codes indicate successful processing, `3xx` codes indicate redirection, `4xx` codes indicate an issue with the request or client context, and `5xx` codes indicate a server-side failure. The exact meaning depends on the individual status code, so an API should choose an appropriate code rather than treating an entire class as one outcome.

How does an HTTP request reach Java backend code?

A client sends an HTTP request to a server endpoint. In a Java web application, the web server or framework receives the request and routes it to the appropriate handler, such as a Spring controller method. The application reads parameters, headers, and possibly the body, performs its business logic, and constructs a response. The framework then serializes the response and sends it back to the client.