HTTP Methods in an API
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:
GET
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 |
POST
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 |
PUT
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 |
PATCH
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 |
DELETE
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 |
What is Idempotent?
An HTTP method is idempotent if multiple identical requests have the same effect as a single request. In other words, making the same request multiple times won't change the outcome after the initial application:
Idempotent Methods: GET, PUT, PATCH, DELETE
Non-Idempotent Method: POST (since making multiple POST requests will create multiple resources)
What is Safe?
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)