GET TO KNOW HTTP
Tags: webdev, programming, beginners, http
Published: 2023-11-07 12:08:32

HTTP is the protocol that enables access to billions of web pages on the internet, the acronym http stands for hypertext transfer protocol and is a layer 7 protocol of the OSI (Open System Interconnection) network stack model. But before we jump right into what is http let's first understand what is a protocol.  

![http request response cycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8x7ieqg2ht1766n0tcl.png) 

A protocol is a set of guidelines or rules that one needs to follow to carry out a specific task, in the context of HTTP, that task can be the transfer of Html, javascript, Css, Video, or Audio.

The early days of the protocol were very simple, so simple that it only supported resource retrieval a.k.a **GET** request, yeah no **POST**, no **PUT**, no **DELETE**, only **GET**, only **GET** was supported.<br /><br />

### How http works<br /><br />  
Imagine that I want to access a web page with a domain like **www.google.com**, my first action will be to open a browser or any other http client I have in hand, type in the url, and hit enter.<br />

The client will then send a request to the Google server, a request is a structured text message that has three parts.

   1. The HTTP verb, the path of the requested resource, and the 
      protocol version. 
   2. Includes headers which are metadata that the client uses to 
      share its state to the server. 
   3. The last one is the body which is simply the data you want 
      to send to the server if it exists.

![HTTP REQUEST STRUCTURE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7bg7bs58x0d1yretdhd.png)

The server on the other hand will receive the request, parse it, and then process it. The server will send back an HTTP response after processing. This response is also a structured text message composed of three parts.

  1. The protocol version and the status code, usually called 
     **status line**.
  2. Includes Headers, which are metadata that the server uses to 
     share information with the client.
  3. The last one is the response body if it exists.


![HTTP RESPONSE STRUCTURE](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vk54caoomwiaa4wrg4q1.png)<br />

### Some http features<br /><br />

**Stateless**: The http protocol does not hold any information about the client's state. This makes it very efficient and scalable, what this means, is that http requests are independent of each other, so they are self-sufficient.

**Request-Response**: The http protocol uses a request-response communication pattern, which means a client sends a request and the server sends back a response.

**Media Independent**: The http protocol can be used to transfer any type of content including Html, Css, javascript, Video, or Audio.

**Cachable**: The http protocol supports caching, which can improve performance by reducing the number of requests sent to the server.

**Security**: http can be used over a secured connection using https.
https allows us to avoid a third party intercepting data being transferred between the client and server, it will secure the communication by applying encryption over data.<br /><br />

### Http methods<br /><br />

An http method is a keyword, that tells the server what kind of action the client wants it to carry out.

![HTTP METHODS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b8j8nitezaoz5gifj2fb.png)

The **GET** method is used by the client to get a current representation of a specified resource.

The **HEAD** method behaves somewhat like the **GET** method, but the only difference is, that a **HEAD** request only gets the status line and headers of the specified resource.

The **OPTIONS** method is called by the client to inquire about available operations on a specified resource or on the server in general.

The **POST** method is called by the client to create a new resource on the server.

The **PUT** method is used to fully update a resource with new data.

The **PATCH** method behaves like the **PUT** method, but the difference is you use it to make a partial update of a resource. It can also be used to create a new resource.

The **DELETE** method is called by the client to delete a resource.

Http methods have different properties and there are some commonalities. Http methods can be safe, or idempotent.

Http-safe methods are methods that do not change the state of the server. Not changing the state of the server means, no resource is created, updated, or deleted from the server. Safe methods are often used to fetch data from the server, such as retrieving a webpage, video, or image.


![HTTP SAFE METHODS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sanruxj8te7b3j490unu.png)

Http idempotent methods are methods that can be repeated multiple times without any side effects(changing server state). For example, sending a subsequent delete request for the same resource will not have an effect after the first delete request is carried out.

![HTTP IDEMPOTENT METHODS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f6l81hb4jj7j89vu8zpu.png)

Safe methods are also considered idempotent, which means that they can be repeated multiple times without any unintended side effects.<br /><br />

HTTP's simplicity, versatility, and widespread adoption have made it the foundation of the World Wide Web. Its ability to transfer various data formats and its support for secure communication have enabled the development of rich and interactive web experiences. As the internet continues to evolve, HTTP will remain an essential protocol for data communication and interaction on the web.