Activity 29: HTTP Methods

HTTP Methods in RESTful APIs
HTTP methods are fundamental for interacting with RESTful APIs. They define the action the client wants to perform on a resource. Below is a detailed overview of the commonly used methods:
GET
Purpose: Used to retrieve data from a server without modifying it. GET requests are idempotent, meaning multiple identical requests should produce the same result.
When to Use: Use GET when you need to fetch data, such as retrieving a list of users or details of a specific user.
Example:
GET /users
GET /users/123
In these examples, the first request retrieves a list of users, while the second retrieves details of the user with ID 123.
POST
Purpose: Used to send data to a server to create a new resource. POST requests are not idempotent, meaning multiple identical requests will result in multiple resources being created.
When to Use: Use POST when you need to create a new resource, such as adding a new user to a database.
Example:
POST /users
{
"name": "Rodel Decio",
"email": "rodeldecio@gmail.com"
}
This request creates a new user with the specified name and email.
PUT
Purpose: Used to update an existing resource on a server. PUT requests are idempotent, meaning multiple identical requests will produce the same result.
When to Use: Use PUT when you need to update an entire resource, such as updating a user's details.
Example:
PUT /users/123
{
"name": "Rodel Decio",
"email": "rodeldecio@gmail.com"
}
This request updates the user with ID 123 with the new name and email.
Difference from POST: While POST is used to create new resources, PUT is used to update existing ones. PUT replaces the entire resource, whereas POST can create a new resource or update parts of an existing one.
DELETE
Purpose: Used to remove a resource from a server. DELETE requests are idempotent, meaning multiple identical requests will produce the same result.
When to Use: Use DELETE when you need to remove a resource, such as deleting a user from a database.
Example:
DELETE /users/123
This request deletes the user with ID 123.
PATCH
Purpose: Used to apply partial updates to a resource. PATCH requests are not necessarily idempotent, but they are designed to modify only parts of a resource.
When to Use: Use PATCH when you need to update part of a resource, such as changing a user's email address without affecting other details.
Example:
PATCH /users/123
{
"email": "rddecio@gmail.com"
}
This request updates only the email address of the user with ID 123.
References
https://restfulapi.net/http-methods/



