DEV Community

Usama
Usama

Posted on

🌐 Understanding HTTP Requests and Responses

When you open a website or interact with an API, your browser or application communicates with a web server using HTTP (HyperText Transfer Protocol). Let’s break down how this communication works step by step.


πŸ”Ή 1. HTTP Requests

Every HTTP interaction starts with a request sent by the client (e.g., your browser).

πŸ“ (a) Request Line

The first line of the request specifies:

  • The method
  • The resource
  • The protocol version

Example:

GET /home.html HTTP/1.1

GET β†’ HTTP method

/home.html β†’ resource being requested

HTTP/1.1 β†’ protocol version

Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ (b) Common HTTP Methods

GET β†’ Request a resource from the server

POST β†’ Send data to the server

PUT β†’ Replace an existing resource

PATCH β†’ Update part of a resource

DELETE β†’ Remove a resource


πŸ“¬ (c) Request Headers

Headers provide extra information about the request.
Example:

Host: example.com
User-Agent: Mozilla/5.0
Accept: text/html
Accept-Language: en
Content-Type: application/json
Enter fullscreen mode Exit fullscreen mode

Host β†’ identifies the target server

User-Agent β†’ tells the server which client is making the request

Accept β†’ specifies which content types are acceptable in response

Accept-Language β†’ preferred language

Content-Type β†’ type of data being sent (e.g., JSON, HTML)


πŸ“¦ (d) Request Body

Often used with POST or PUT to send data:

POST /users HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "name": "Usama",
  "age": 23
}

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 2. HTTP Responses

Once the server processes the request, it replies with an HTTP response.

🏷️ (a) Status Line

Example:

HTTP/1.1 200 OK

HTTP/1.1 β†’ protocol version

200 β†’ status code

OK β†’ reason phrase

Enter fullscreen mode Exit fullscreen mode

πŸ“Š (b) Common Status Codes

1xx Informational β†’ Request received (e.g., 100 Continue)

2xx Success β†’ Request succeeded (e.g., 200 OK, 201 Created)

3xx Redirection β†’ Resource moved (e.g., 301 Moved Permanently, 302 Found)

4xx Client Error β†’ Problem in request (e.g., 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found)

5xx Server Error β†’ Problem on server side (e.g., 500 Internal Server Error, 503 Service Unavailable)


πŸ“‘ (c) Response Headers

Provide metadata about the response:

Date: Tue, 10 Sep 2025 12:00:00 GMT
Server: Apache/2.4.1 (Linux)
Content-Type: text/html
Content-Length: 120
Enter fullscreen mode Exit fullscreen mode

Date β†’ when the response was generated

Server β†’ server software details

Content-Type β†’ type of returned content

Content-Length β†’ size of the response body


πŸ“„ (d) Response Body

This is the actual content returned (HTML, JSON, images, etc.):

<html>
  <head><title>Hello</title></head>
  <body>Hello Usama!</body>
</html>

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 3. Full Example: Request β†’ Response Flow

Client Request

GET /index.html HTTP/1.1
Host: example.com
User-Agent: Chrome/120.0
Accept: text/html
Enter fullscreen mode Exit fullscreen mode

Server Response

HTTP/1.1 200 OK
Date: Tue, 10 Sep 2025 12:00:00 GMT
Server: Apache/2.4.1 (Linux)
Content-Type: text/html
Content-Length: 120

<html>
  <body>Hello World!</body>
</html>
Enter fullscreen mode Exit fullscreen mode

βœ… Summary

An HTTP Request = Method + Headers + (optional) Body

An HTTP Response = Status Line + Headers + Body

Status Codes quickly tell you whether a request was successful, redirected, invalid, or failed

Understanding this flow is essential for working with web development, APIs, and debugging client-server communication.


πŸ’‘ Now you’re ready to inspect, debug, and even build APIs with confidence!

Top comments (1)

Collapse
 
usama_dev profile image
Usama

This information I took in meta front end course available in Coursera