HTTP🌐 Methods🔨: GET 📖 and POST 📝 in 5 Minutes
Tags: codenewbie, html, webdev, beginners
Published: 2020-11-29 12:06:28

---
title: HTTP🌐 Methods🔨: GET 📖 and POST 📝 in 5 Minutes
published: true
description: 
tags: #codenewbie #html #webdev #beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/prokrtw92tt3j2v3iutw.jpg
---

🚧***I am writing this because I, myself have been really confused about how GET, POST methods works at all. This article is mainly an explanation of what I have learnt so far. This article solidifies my knowledge. If anyway this article turns out to be helpful to you, please let me know in the comment section below. 
Thank you!***🚧

### Contents:📃
1. [What is HTTP](#HTTP)
2. [HTTP Methods](#HTTP_methods)
3. [Small talk about CRUD](#crud)
4. What is:
	* [GET](#get)
	* [POST](#post)
	* [PUT](#put)
	* [DELETE](#delete)
5. [Safe methods and Unsafe methods](#safe_unsafe)
6. [Idempotent methods](#idempotent)

## <a id = "HTTP">What is HTTP?🌐</a>

`HTTP` means Hypertext Transfer Protocol. `HTTP` is a protocol used to fetch resources like `HTML` documents. This client-server protocol is the foundation of any data exchange on the Web. Using this protocol a complete document is reconstructed by fetching sub-documents like images, texts, videos and scripts. 

Clients and servers usually communicate using messages and these messages are named depending on what action they are performing. If the message was sent from the client's side to the server then it is called a request and if it is received from the server-side on the client-side, then it is called a response. 

⚠️*Spoiler alert, `GET` and `POST` are both related to this request and response things.*
## <a id = "HTTP_methods">HTTP🌐 Methods🔨</a>
`HTTP methods` or to be more precise `HTTP request methods` are a defined set of request method used to indicate the desired action to be performed on a given resource. 

It doesn't matter if the name of the methods are specifically noun or not, but sometimes these methods are also referred to as `HTTP verbs`(as they are performing an action from the client's side). 

The most common methods are as follows:

    * GET
    * POST
    * PUT
    * DELETE

#### <a id = "crud">Are you aware of CRUD: it's related 🤔</a>
**C**reate **R**ead **U**pdate **D**elete

**CRUD** is essentially refereed to as a structural reference for building a service or an application. 

A **CRUD** application should be able to create, read, update and delete data.  The reason why I am talking about **CRUD** here is that the four HTTP methods that I mentioned before are just **CRUD** enabler. 

**C**reate = `POST`

**R**ead = `GET`

 **U**pdate = `PUT`

  **D**elete = `DELETE`
  
### <a id = "get">GET Method📖🔨 </a>

> The `GET` method bundles the submitted data into a string and uses
> this to compose a URL. That URL contains the address where the data
> must be fetched from. The sent data also contains the data keys and
> values. Requests using `GET` should only retrieve data.

For example in a `<input>` tag you have entered a value and clicked the submit button. Now if the method of the form is `GET`, the form will create a URL using that value you entered. That URL will also contain the key-value pair of the data for locating the data. 

### <a id = "post">POST Method📝🔨 </a>

> The `POST` method bundles up the form data, encodes it for
> transmission and sends it to the server followed by receiving a
> response. `POST` method often causes a change in state or side effects
> on the server.

Any request that could be used to change the state of the system - for example, a request that makes changes in the database - should use `POST`. `GET` should be used only for requests that do not affect the state of the system.

### <a id = "put">PUT Method📤🔨 </a>

> The `PUT` method, just like the `GET` and `POST` methods works from
> the client's side. `PUT` method sends a request with a key and value to
> locate the position the data the needs to be updated. `PUT` does
> not create new data. `PUT` updates existing data with new passed on
> value.

### <a id = "delete">DELETE Method🗑️🔨 </a>

> `DELETE` method, as the name suggests, deletes the specified data.

Based on the type of actions being performed by these different methods, there are 2 types of methods. 
## <a id = "safe_unsafe">Safe methods🛡️ and Unsafe☣️ methods🔨 </a>
⚠️*By safe or unsafe I don't mean that you shouldn't use the unsafe methods. Unsafe method is a name given to particular types of methods for there working nature. Use it where you must need it.*⚠️

Methods that, after sending the request from the client to the server, doesn't change the state of the data is called Safe method. This kind of methods is used to only fetch a specified data without updating or deleting the original data in real-time. 

`GET` is a Safe method because `GET` is used to fetch the data, nothing else. 

Methods used to send a request to change the state of the original data on the server is called Unsafe methods. Unsafe methods don't just fetch data but also can delete, update or create new data. 

> `PUT, DELETE, POST` are Unsafe methods.

## <a id = "idempotent">Idempotent methods🪤🔨</a>
***Scenario:*** Suppose there are 8 data sets. Data sets about 3 dogs and there ages.
```python 
data_set = {
"dog1": 12,
"dog2": 5,
"dog3": 10
}
```
* You send 5 requests using `PUT` to the server to update `"dog3"` value from 10 to 5. 
* After sending the first `PUT` request the `"dog3"` key will have its value changed to 5. 
* But those 4 other `PUT` requests will not change the data as the original request was to change the value 10 to 5 and the data is already changed by the first request. 

One change of state of the server data occurred in spite of making multiple requests. 

***Result:*** Same thing happens when you are using `GET` and `DELETE` methods. Several requests with the one same change of the data in the server. This is why `GET`, `DELETE` and `PUT` are called Idempotent methods. 

>But the `POST` method is different. Here notice the similarities between `GET`, `PUT` and `DELETE` methods. All three of them have the same kind of requests where they don't create new data in the server upon a request from the client's side. But `POST` does. 

## Conclusion(for me😂): `POST`📝 and `PUT`📤 are totally different

`POST` method is used to create new data in the server-side though the same data might already exist on the server. 

So when you use `POST` to change the data of `"dog3"` from 10 to 5, it won't work. `POST` will create another data set with `"dog3"` as key and 5 as value. And 5 requests of `POST` will try to create 5 data-sets of the `"dog3"` key-value pair. 

`POST` changes the state of the server in real-time and multiple times based on the amount of request is being pushed using `POST` from the client's side. 

## Thank you🙇‍♂️🙏
I am soo happy☺️ that you read all of it and now you are here. We can connect on [Facebook](https://www.facebook.com/arunavaskar/)☹️📘 and [Twitter](https://twitter.com/arunavaskar)🐦. 
You can check out my profile for more of my writings.