HTTP Protocol Requests and Responses: Communication between Client and Server

·

2 min read

Definition

HTTP (Hyperext Transfer Protocol) is one of the communication protocols used on the internet to exchange data. HTTP is responsible for communication between web browsers and web servers, where the client requests data from the server and the server responds with the data.

How it Works

HTTP works on a request-response model, where the client typically requests a web page and the server responds with the requested HTML file, images, videos, and other resources.

Components of the HTTP Protocol

  1. HTTP Method: The method used by the client to send a request to the server. The most common HTTP methods are GET, POST, PUT, and DELETE.

  2. URI (Uniform Resource Identifier): A string used to uniquely identify a resource on the internet.

  3. Header: The part of the request or response that contains metadata. Headers can contain information about the request or response, authentication information, and cache control information.

  4. Body: The part of the request or response that contains the actual data being transmitted. Generally, the body is not used in a GET request, but in a POST or PUT request, data is sent in the body.

HTTP Request Example

[http method] [URL] [HTTP version]

[header]

GET /index.html HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
  • HTTP Method: GET

  • URI: /index.html

  • Version: HTTP/1.1

  • Header: Host, User-Agent, Accept, Accept-Language, Connection

HTTP Response Example

[HTTP version] [status code]

[Header]

[Body]

HTTP/1.1 200 OK
Date: Wed, 12 May 2021 10:12:34 GMT
Server: Apache/2.4.29 (Unix)
Last-Modified: Fri, 01 May 2020 12:34:56 GMT
ETag: "abcd1234efgh5678"
Content-Length: 1234
Content-Type: text/html

<!DOCTYPE html>
<html>
<head>
    <title>Example Web Page</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <p>This is an example web page.</p>
</body>
</html>
  • Version: HTTP/1.1

  • Status Code: 200 OK

  • Header: Date, Server, Last-Modified, ETag, Content-Length, Content-Type

  • Body: HTML document

Did you find this article valuable?

Support Eunhan's blog by becoming a sponsor. Any amount is appreciated!