When should you use GET, POST, PUT and DELETE methods?
GET, POST, PUT and DELETE methods are most commonly used HTTP methods.
Even though you could use the methods in any perspective you like, I’m going to share what are considered the standard ways of using the above HTTP methods.
For the sake of understanding, let us consider a back-end application which is connected to a relational database.
GET method:retreieving
This method should be used when you are querying the data from the database and presenting it to the client. Generally, GET method does not modify anything in the database level. This method should only used for APIs which fetch the data.
POST method:
This method should be used when you are actually inserting a record inside the table. Here we are changing the state of the database. We are actually POSTING data to the database to create a new record. Hence a POST method should be used.
PUT method:
This method should be used when you are updating an already existing record in the table. Here you are not retrieving any data or inserting new data. Instead, we are updating the existing data. For this purpose a PUT call shall be made.
DELETE method:
This method should only be used when your API actually is designed to delete records or data on demand. Keep in mind, that not a lot of DELETE mapping methods are used in Production environment as it quite risky.
That was a short introduction for HTTP methods.