HTTP methods, also known as verbs, define the type of action to be performed on a given resource. Each method provides a different type of functionality. Here are the commonly used HTTP methods in an API:
Description: The GET method is used to retrieve data from a server.
| Property | Value |
|---|---|
| Use Case | Fetching data, such as a list of users or a specific user profile. |
| Idempotent | Yes |
| Safe | Yes |
Description: The POST method is used to send data to a server to create a new resource.
| Property | Value |
|---|---|
| Use Case | Creating a new user or submitting a form. |
| Idempotent | No |
| Safe | No |
Description: The PUT method is used to update an existing resource or create a new resource if it doesn't exist.
| Property | Value |
|---|---|
| Use Case | Updating user details or replacing a specific resource. |
| Idempotent | Yes |
| Safe | No |
Description: The PATCH method is used to apply partial modifications to a resource.
| Property | Value |
|---|---|
| Use Case | Updating specific fields of a user profile. |
| Idempotent | Yes |
| Safe | No |
Description: The DELETE method is used to remove a specified resource from a server.
| Property | Value |
|---|---|
| Use Case | Deleting a user or removing a specific item. |
| Idempotent | Yes |
| Safe | No |
An HTTP method is idempotent if making multiple identical requests produces the same result as making a single request. In other words, calling an idempotent method multiple times will leave the server in the same state as calling it once.
Examples:
- PUT - Updating a user's name to "John" once or five times results in the same state: the name is "John"
- DELETE - Deleting a resource once or five times results in the same state: the resource is deleted
- GET - Retrieving data multiple times doesn't change the server state
Idempotent Methods: GET, PUT, PATCH, DELETE
Non-Idempotent Method: POST - Making multiple POST requests will create multiple new resources, resulting in different states each time
An HTTP method is safe if it doesn't alter the state of the server. Safe methods are typically used for retrieving data rather than modifying it:
Safe Method: GET (as it only retrieves data without changing the server state)
Non-Safe Methods: POST, PUT, PATCH, DELETE (as they alter the state by creating, updating, or deleting resources)