Demystifying HTTP: The Secret Language of the Web
Tags: webdev, http, node, web
Published: 2024-03-28 08:00:00

In the early days of the web, there was no standard way for the browser to connect to a server. The functionality of web applications was limited because the HTTP protocol lacked valid support for various actions such as submitting form data, updating resources, or deleting resources. After all, that was beyond the simple HTML document retrieval.

HTTP methods were created to provide a uniform language for communicating on the web. These methods are globally understood by the browser and used to communicate with the server regarding what information is required or what action should be taken. There are many HTTP methods to choose from, each of which allows browsers and servers to interact with each other in various ways.

The purpose of this article is to provide you with a better understanding of the many HTTP protocols available, how data travels across the web, and how browsers and servers work together to gather accurate data.

## What is HTTP?
HTTP stands for Hypertext Transfer Protocol, and it serves as a communication channel between the browser and the web server. When a website's URL is entered into a browser, it sends an HTTP request to the server. The server receives the information from the request and sends an HTTP response back to the browser, which then displays the necessary information.

## Evolution of HTTP

During the early days of the internet, HTTP/0.9 was a simple protocol for retrieving only HTML documents. As the web evolved, HTTP/1.0 was introduced as a more robust way to communicate between browsers and servers. It included features such as headers and status codes, but it lacked efficiency and performance, particularly when loading multiple resources simultaneously.

HTTP/1.1 was then designed to address that issue with various enhancements such as persistent connections and caching, which dramatically improves web surfing performance. HTTP was later updated to accommodate richer content and more complicated interactions. There were more advancements in HTTP/2, including multiplexing, header compression, and server push, which improved performance and minimized latency.
Today, HTTP not only retrieves HTML web documents but also accesses databases, images, and web content. These resources are accessible with the help of APIs, and we send requests to the API and receive responses through the HTTP protocol, which allows for seamless exchange of data on the web.

## How is Data Exchanged on the Web?

When we open our favorite search engine and type in a web address such as [www.hashnode.com](www.hashnode.com) or [www.twitter.com](www.twitter.com), we send a request message to the server in charge of serving those resources, and the server answers by sending a response message.

![image of how data is exchanged on the web](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uafqlx2nqpef6ujrsda2.jpg)

Assuming we were building a business listing application, there would be a search bar, a table to list all businesses, and the ability to search by name, state, and city. Having all business information manually coded into the business listing app would slow down the app and it would be difficult to update manually.

Instead, the app could use an API to obtain the business listing data. The app would determine the user's location, then send a request to the server and show businesses of all kinds available in the user’s location. Users can choose from a variety of request methods depending on what they are trying to accomplish with the app. A response from the server would include business information plus a few more details, depending on the API.

To keep track of requests associated with that address, the business listing app communicates with a server application located somewhere. When it receives a request, it works to fulfill it by either reading from a database, using another API, or performing a programmatic computation using the data provided by the user.

## Types of HTTP Methods

HTTP is a protocol that allows the browser and the server to exchange requests and responses. It is important to note that while each method implements a different semantic, they all share some common characteristics.

### POST Request 

The POST request submits an entity to a specified resource, and the entity data is stored in the HTTP request body when making the request. Successful POST requests will return a 201 response code.
 
To add new business data to our listing API, we can use the POST method.

### GET Request

A GET request is used to retrieve data from a specified resource. A successful GET request has a status code of 200 and returns a response providing the requested information. Depending on how the API is structured.

In our business listing app, we can use a GET method to obtain business information available for a certain city or the entire country. 

### PUT Request 

The PUT requests are used to update an entity of a specific resource available on the server. The PUT request updates the entire entity with the data given in the HTTP request body. If we want to update a portion of our resource with a PUT request, we must include the entire resource's data. Otherwise, it will create a new resource. The next HTTP request will demonstrate how to overcome this obstacle.

In our business listing app, we can update each business' information using the PUT method.

### PATCH Request 

In a PATCH request, a resource is partially updated. We only need to provide the data that needs updating when using the PATCH method. 
When a PATCH request is made, only the field that needs to be updated is updated, whereas when a PUT request is made, the whole resource is updated, even if only one field needs to be changed.
 
To update the address of a business in our business listing app, we can use the PATCH method.

### DELETE Request

We use the DELETE request to remove a specific entity or resource from the server. The business listing app allows us to remove a business from the app if we no longer wish to list it.

## CONCLUSION

In this tutorial, we have learned about how the browser communicates with the server and how we can use HTTP requests available to exchange data on the web. If you have found this helpful, please consider sharing it with others who might benefit.

Thank you for Reading.