๐ 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:
"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:
๐ป 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);
}
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" }),
}
This includes, of course the three main parts of a HTTP request.
๐ถ TCP/IP Model (Whereโs HTTP?)
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
MDN Web Docs โ HTTP Overview โ The most complete and reliable documentation for web protocols.
MDN Web Docs โ fetch() API โ Official reference for making HTTP requests in JavaScript and TypeScript.
๐ If you found this article interesting or want to get in touch, Iโm active on X @mirchezz
Top comments (0)