Types of Requests GET, POST, PUT, PATCH, DELETE
To connect with our server, we send different types of requests.
1. GET Request β Fetch Data from Server
- The GET request is used to retrieve (fetch) information from a server.
- This is the most commonly used request when an application needs to read data without making any changes to it.
π Example Use Cases:
β
Fetching a user profile from a database.
β
Retrieving a list of posts from a blog.
β
Getting weather updates from an API.
π Example Request:
This will fetch the data of a user with ID 123 from the server.
π Example Response from Server:
2. POST Request β Add New Data to Server
- The POST request is used when we want to send new data to the server and store it in a database.
- Unlike GET, a POST request modifies the serverβs data.
π Example Use Cases:
β
Signing up a new user.
β
Adding a new blog post.
β
Submitting a contact form.
π Example Request:
- This will create a new user with the given details in the database.
π Example Response from Server:
3. PUT Request β Update Entire Data
- The PUT request is used to update an entire record in the server.
- If a record exists, it gets updated; if not, a new record is created.
- All fields must be included in the request, even if only one value is being changed.
π Example Use Cases:
β
Updating user profile information.
β
Replacing old product details with new ones.
π Example Request:
- This will replace the entire user record in the database.
π Example Response from Server:
4. PATCH Request β Update Specific Fields
- The PATCH request is similar to PUT but only updates specific fields instead of replacing the entire record.
- This is useful when only a few changes are needed instead of resending the entire dataset.
π Example Use Cases:
β
Updating only the email of a user.
β
Changing only the price of a product.
π Example Request:
- This will only update the email of the user and keep other details unchanged.
π Example Response from Server:
5. DELETE Request β Remove Data from Server
- The DELETE request is used to remove a specific resource from the server.
- Once deleted, the data cannot be retrieved unless the system has a backup.
π Example Use Cases:
β
Deleting a user account permanently.
β
Removing a blog post from a website.
β
Deleting an order from a shopping cart.
π Example Request:
- This will delete the user with ID 123 from the server.
π Example Response from Server:
Summary Table β Quick Comparison
Request Type | Purpose | Example Use Case |
GET | Fetch data from server | Get user profile, retrieve blog posts |
POST | Add new data to server | Create a new user, add a new post |
PUT | Replace entire data | Update all details of a user |
PATCH | Update specific fields | Change only the email or password |
DELETE | Remove data from server | Delete a comment, remove an order |