Activity 29: HTTP Methods
GET:
Purpose: Retrieve data from the server. It does not alter any data, making it a safe and idempotent method.
Usage: Ideal for fetching a list of resources (e.g., a list of users) or retrieving a single resource by ID. For instance,
GET /usersmight return all users, whileGET /users/123would retrieve information about a specific user.Example: Fetching data from an online store’s product catalog.
POST:
Purpose: Send data to the server to create a new resource. Unlike GET, POST can modify the server’s data.
Usage: Commonly used for creating new records (e.g., submitting a new user registration form).
Example:
POST /userswith data{ "name": "John Doe" }creates a new user named John Doe.Note: Unlike PUT, POST requests are not idempotent; repeated submissions can create multiple entries.
PUT:
Purpose: Update an existing resource, or create it if it does not exist. PUT is often used for replacing a resource entirely.
Usage: When you want to modify a resource’s data, or add one if it’s not present.
PUT /users/123would replace user 123’s data.Difference from POST: While POST is used for creating new resources, PUT is used for updating or creating at a specific URL. PUT requests are idempotent, meaning multiple identical requests have the same effect as a single one.
Example: Updating user information or uploading a file to replace the existing one.
DELETE:
Purpose: Remove a specified resource from the server.
Usage: Often used for deleting entries, such as removing a user account with
DELETE /users/123.Example: Deleting a post from a blog or a product from an inventory.
PATCH:
Purpose: Apply partial modifications to a resource, as opposed to the full replacement performed by PUT.
Usage: When updating only specific fields of a resource, such as changing a user’s email address without altering other details. For example,
PATCH /users/123might include{ "email": "newemail@example.com" }.Difference from PUT: PATCH allows you to update just part of the resource, while PUT requires sending the complete resource data.
Example: Partially updating a user's profile settings, like changing only the password.