Skip to main content
Networking

HTTP Methods, Status Codes, and Headers Explained for Developers

5 mins

Abstract diagram of interconnecting computers communicating via HTTP.

HTTP Request and Response #

The Hypertext Transfer Protocol (HTTP) is a stateless communication protocol that controls the exchange of data between clients and servers on the web. Stateless means that each request from a client to a server is treated as an independent transaction. The server does not retain any information about previous requests, which simplifies the protocol and makes it easier to scale.

HTTP defines how requests are made and responses are returned, enabling the seamless transfer of resources like HTML, images, and APIs over the internet.

Whenever you type a url or click a link in your browser, a HTTP request is sent to the server. The server processes your request and sends back a HTTP response, containing the HTML page you requested, which is then rendered in your browser.

HTTP Request Structure #

For the server to process a request, the server requires

  • The resource being requested
  • What action is to be performed
  • Any data that is required to perform the action

The HTTP request structure consists of the following components:

Request Line #

The request line contains the following elements:

  • Method: The HTTP method, such as GET, POST, PUT, DELETE, or PATCH, that specifies the action to be performed on the resource.
  • URI: The Uniform Resource Identifier (URI) that identifies the resource being requested.
  • HTTP Version: The version of the HTTP protocol being used, such as HTTP/1.1.

The method types are typically used for the following purposes:

  • GET: Retrieve data from the server, and when clicking a link or typing a URL in the browser, a GET request is sent to the server.
  • POST: Submit data to the server, such as form submissions or API calls. Login forms, registration forms, and search queries are examples of POST requests.
  • PUT: Update an existing resource on the server. For example, updating a user’s profile information.
  • DELETE: Remove a resource from the server. For example, deleting a user account.
  • PATCH: Partially update a resource on the server. For example, updating only the user’s email address.

A server application may use some or all of these methods to handle different types of requests, and may also combine actions. e.g. use post to create a new resource and put to update an existing resource.

Headers #

HTTP headers influence web requests and responses by providing important metadata that controls how data is processed, such as content type, caching, and authentication. For example, headers like Content-Type specify the format of the response, while Authorization handles user credentials for secure access.

Some common headers include:

  • Content-Type: The MIME type of the request body, such as application/json or text/html.
  • Content-Length: The size of the request body in bytes. i.e. how much data is being sent to the server.
  • Accept: The MIME types that the client can accept in the response.
  • User-Agent: Information about the client making the request, such as the browser or device type.
  • Authorization: Credentials for secure access to the server, such as a token or username and password.

The header format is key-value pairs separated by a colon, such as Content-Type: application/json.

Body #

The request body contains the data being sent to the server, such as form data or JSON payloads. Note that the body is optional, and not all requests have a body. POST and PUT requests typically include a body, while GET requests do not.

Example HTTP Request #

An example GET request for the index.html page on www.example.com might look like this:

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

Note that no body is included in this request, as no information other than the resource being requested is required by the web server.

POST requests are often used to submit data to a server, such as form submissions or API calls. An example POST request to the /api/data endpoint on www.example.com might look like this

POST /api/adduser HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3
Accept: application/json
Content-Type: application/json
Content-Length: 35

{
  "name": "John Smith",
  "age": 30
}

For this request, the body contains the data being sent to the server. The server knows when no more data is expected by the Content-Length header.

HTTP Response Structure #

The web server will respond with a HTTP response, which contains the requested resource or an error message if the request was unsuccessful. The HTTP response structure consists of the following components:

Status Line #

The status line contains the following elements:

  • HTTP Version: The version of the HTTP protocol being used, such as HTTP/1.1.
  • Status Code: A three-digit code that indicates the outcome of the request. Common status codes include 200 (OK), 404 (Not Found), and 500 (Internal Server Error).
  • Status Message: A brief description of the status code, such as OK, Not Found, or Internal Server Error.

Headers #

HTTP headers provide additional information about the response, such as the type of data being returned, the size of the response body, and the server making the response. Some common headers include:

  • Content-Type: The MIME type of the response body, such as application/json or text/html.
  • Content-Length: The size of the response body in bytes.
  • Server: Information about the server making the response, such as Apache or Nginx.

Body #

Similar to the request body, the response body contains the data being returned by the server, such as HTML content, images, or JSON payloads. The body is optional, and not all responses have a body. i.e. some error responses may not have a body such as 404 Not Found.

Example HTTP Response #

An example response to the GET request for the index.html page might look like this:

HTTP/1.1 200 OK
Date: Tue, 12 Oct 2024 01:20:00 GMT
Server: Apache/2.4.6 (CentOS)
Content-Type: text/html
Content-Length: 120

<!DOCTYPE html>
<html>
<head>
  <title>Example Page</title>
</head>
<body>
  <h1>Welcome to the Example Page</h1>
</body>
</html>