DEV Community

Cover image for HTTP🌐 Methods🔨: GET 📖 and POST 📝 in 5 Minutes
Arunava Ch.
Arunava Ch.

Posted on

HTTP🌐 Methods🔨: GET 📖 and POST 📝 in 5 Minutes

🚧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
  2. HTTP Methods
  3. Small talk about CRUD
  4. What is:
  5. Safe methods and Unsafe methods
  6. Idempotent methods

What is HTTP?🌐

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.

HTTP🌐 Methods🔨

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
Enter fullscreen mode Exit fullscreen mode

Are you aware of CRUD: it's related 🤔

Create Read Update Delete

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.

Create = POST

Read = GET

Update = PUT

Delete = DELETE

GET Method📖🔨

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.

POST Method📝🔨

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.

PUT Method📤🔨

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.

DELETE Method🗑️🔨

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.

Safe methods🛡️ and Unsafe☣️ methods🔨

⚠️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.

Idempotent methods🪤🔨

Scenario: Suppose there are 8 data sets. Data sets about 3 dogs and there ages.

data_set = {
"dog1": 12,
"dog2": 5,
"dog3": 10
}
Enter fullscreen mode Exit fullscreen mode
  • 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☹️📘 and Twitter🐦.
You can check out my profile for more of my writings.

Top comments (3)

Collapse
 
liyasthomas profile image
Liyas Thomas

Very informative post. Feel free to make use of Hoppscotch - an open sourced {free} API request builder. Helps you to spin up any API endpoint and fetch responses in seconds.

GitHub logo hoppscotch / hoppscotch

👽 A free, fast and beautiful API request builder used by 100k+ developers. https://hoppscotch.io

Collapse
 
alex_yetto profile image
Alex Yetto

Would be cool if you also covered PATCH

Collapse
 
onowdev profile image
agssugiono

Super Sekali, Terima Kasih Atas Artikel ini, Jadi menambah Ilmu Saya,