DEV Community

Cover image for HTTP? ๐Ÿค” The Web's Unsung Hero Explained
Miguel Miranda
Miguel Miranda

Posted on

HTTP? ๐Ÿค” The Web's Unsung Hero Explained

๐ŸŒ A Bit of History โ€” Where HTTP Came From

Back in 1989, Tim Berners-Lee invented the World Wide Web (WWW). But it wasnโ€™t just websites โ€” it was a system of protocols, and one of them was HTTP: the HyperText Transfer Protocol.

HTTP was designed as a simple way to send hypertext (HTML) documents between computers. This allowed the user to experience a more dynamic and interactive way to access information.

Since then, HTTP has evolved (currently at HTTP/3), but its core idea hasnโ€™t changed:

Image of server and client conecting

"Let the client ask, and let the server answer."

That basic principle powers nearly every web app today. Without HTTP, the internet would not look, feel, or behave as it does.


Request vs response:

๐Ÿ“ก What Is an HTTP Request and Response?

When your browser opens a website, it sends an HTTP request.

The server replies with an HTTP response.

๐ŸŸฆ Request Structure

An HTTP request typically includes:

  • Start line: Method (GET, POST, PUT, DELETE), path, protocol version
  • Headers: Info like content type, length, user-agent, tokens.
  • Body: Optional โ€” used in methods like POST or PUT to send information to the server.

๐ŸŸจ Response Structure

A server sends back:

  • Start line: Protocol version + status code like so:

1xx - Informational

2xx โ€“ Success

3xx โ€“ Redirection

4xx โ€“ Client Error

5xx โ€“ Server Error

  • Headers: Metadata like content-type, caching rules
  • Body: Often contains the HTML or JSON payload

๐Ÿ“ธ Request vs Response diagram:

Request-Response HTTP diagram


๐Ÿ’ป Example: Making an HTTP Request in TypeScript

Letโ€™s say we want to send a POST request with JSON to an API:

async function sendData() {
  const response = await fetch("https://api.example.com/data", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ name: "Miguel", email: "miguel@example.com" }),
  });

  if (!response.ok) {
    console.error("Request failed with status:", response.status);
    return;
  }

  const data = await response.json();
  console.log("Response data:", data);
}
Enter fullscreen mode Exit fullscreen mode

As we can see the fetch sent a Post Request:

{
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ name: "Miguel", email: "miguel@example.com" }),
  }
Enter fullscreen mode Exit fullscreen mode

This includes, of course the three main parts of a HTTP request.


๐Ÿ“ถ TCP/IP Model (Whereโ€™s HTTP?)

HTTP in TCP/IP

HTTP sits at the Application layer of the TCP/IP model:

Application โ† HTTP lives here ๐ŸŒ
Transport โ† TCP handles the delivery ๐Ÿ“ฆ
Internet โ† IP takes care of addresses ๐Ÿ“ฎ
Link โ† Ethernet/WiFi physically connects โšก


Key Things to Keep in Mind:

HTTP is a stateless protocol.

  • Each request is independent. The server doesnโ€™t remember the last thing you asked.
  • The connection is not bidirectional meaning the server cannot send request by himself to the client.
  • If you want persistent connections, you need tools like WebSockets, which are also another type of protocol we use daily, like in videogames, videocalls or chats.

๐Ÿง  Final Thoughts
HTTP may feel invisible, but itโ€™s everywhere. It silently handles the exchange of information between browser and server.


๐Ÿ“š References

๐Ÿ‘‹ If you found this article interesting or want to get in touch, Iโ€™m active on X @mirchezz

Top comments (0)