Last updated

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.

PropertyValue
Use CaseFetching data, such as a list of users or a specific user profile.
IdempotentYes
SafeYes

POST

Description: The POST method is used to send data to a server to create a new resource.

PropertyValue
Use CaseCreating a new user or submitting a form.
IdempotentNo
SafeNo

PUT

Description: The PUT method is used to update an existing resource or create a new resource if it doesn't exist.

PropertyValue
Use CaseUpdating user details or replacing a specific resource.
IdempotentYes
SafeNo

PATCH

Description: The PATCH method is used to apply partial modifications to a resource.

PropertyValue
Use CaseUpdating specific fields of a user profile.
IdempotentYes
SafeNo

DELETE

Description: The DELETE method is used to remove a specified resource from a server.

PropertyValue
Use CaseDeleting a user or removing a specific item.
IdempotentYes
SafeNo

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)