Skip to main contentLogo

Command Palette

Search for a command to run...

Http Request & Http Response

Published on
Mar 25, 2025
Http Request & Http Response

HTTP Request

A message sent by the client to request data from the server or to ask it to perform a certain operation.


Three Parts of the HTTP Request Structure

Request Line

This is the first and mandatory part of the request. It consists of three main components:

  1. Method – (GET, POST, PUT, DELETE, etc.)
  2. Request URL/Path – (e.g., /api/users/123?q=http&page=2)
  3. HTTP Version – (HTTP/1.1, HTTP/2)

Headers

Additional information — i.e., metadata — is sent here. Metadata is in key-value form and provides extra details about the request.

Most commonly used HTTP headers:

  • Host
  • User-Agent
  • Accept
  • Content-Type
  • Content-Length
  • Authorization
  • Cookie

Real case example:

POST /api/user HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Accept: application/json
Content-Type: application/json
Content-Length: 58
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Cookie: SESSIONID=abc123xyz

Message Body / Payload

After the headers, a blank line is left and the body is provided if present. It is mainly used in requests that send data to the server, such as POST, PUT, PATCH.

The body is written in the format specified by the Content-Type header, e.g., application/json.

General HTTP Request Format:

POST /api/orders HTTP/1.1
Host: www.example-store.com
User-Agent: curl/7.79.1
Accept: application/json
Content-Type: application/json; charset=utf-8
Content-Length: 54
Authorization: Bearer dG9rZW4gZXhhbXBsZQ==

{
  "productId": 42,
  "quantity": 2,
  "customer": "John Doe"
}

HTTP Response

The response returned to an incoming HTTP request from the client. Its purpose is to:

  • Indicate the outcome of the request (successful? error? redirection?)
  • If successful, return the requested result/data to the client

Three Parts of the HTTP Response Structure

Status Line

The first and mandatory part of the response. It consists of three parts:

  1. HTTP Version – (HTTP/1.1, HTTP/2)
  2. Status Code – A three-digit code returned by the server.
  3. Reason Phrase – A human-readable description of the status (e.g., OK, Not Found, Internal Server Error)

Status Code Categories:

CategoryMeaning
1xxInformational – The operation is in progress
2xxSuccess – The request was successfully processed
3xxRedirection – A redirection is required
4xxClient Error – An error on the client side
5xxServer Error – An error occurred on the server

Headers

Additional information (metadata) sent by the server to the client, also in key-value format.

Most commonly used response headers:

  • Content-Type – Type of the returned data (application/json, text/html, etc.)
  • Content-Length – Length of the body (in bytes)
  • Set-Cookie – To send a cookie to the client
  • Cache-Control – Caching directives for the response
  • Date – The date when the response was sent
  • Server – Technical information about the server
  • Location – Provides the new URL for redirection (with 301, 302)

Real case example:

HTTP/1.1 200 OK
Date: Sun, 06 Apr 2025 10:22:35 GMT
Content-Type: application/json
Content-Length: 72
Set-Cookie: SESSIONID=xyz123abc; HttpOnly
Server: Apache/2.4.46 (Ubuntu)

Message Body / Payload

If the server returns data as a response, that data is provided in the body.

📌 Only some status codes (200, 201, etc.) include a body.

For example, in statuses like 204 No Content and 304 Not Modified, there is no body.

Example response in JSON format:

{
  "userId": 123,
  "username": "john_doe",
  "email": "john@example.com"
}
Thanks for reading.