PUT method

PUT is one of the HTTP request methods and is used to create or update a resource at the specified URI location. In other words, the PUT method essentially saves the body content sent in the request as a resource at the location of the URI path. RESTful APIs often use PUT to update resources.

Features of the PUT method

  • The request URI is used as the resource identifier
  • The request body contains the entire updated resource
  • It has idempotency – repeating the same request yields the same result
  • If the existing resource does not exist, a new one will be created
  • If an existing resource exists, it will be completely replaced with the contents of the body

Example: Write the code example here having sample data that will be updated.

Javascript




const express = require("express");
const app = express();
app.use(express.json());
 
// Sample data
let users = [
    { id: 1, name: "John Doe", email: "john.doe@example.com" },
    { id: 2, name: "Jane Smith", email: "jane.smith@example.com" },
];
 
// PUT request to update user data
app.put("/users/:id", (req, res) => {
    const id = parseInt(req.params.id);
    const { name, email } = req.body;
    let userFound = false;
 
    users = users.map((user) => {
        if (user.id === id) {
            userFound = true;
            return { ...user, name, email };
        }
        return user;
    });
 
    if (userFound) {
        res.status(200).json({ message: "User updated successfully", users });
    } else {
        res.status(404).json({ message: "User not found" });
    }
});
 
const PORT = 3000;
app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});


Output:

Advantages of PUT Method

  • It helps you to store the supplied entity under the supplied URI.
  • If the supplied entity already exists, then you can perform the update operation, or you can create with that URI.
  • You can create a resource as many times as you like.
  • Creating a resource with PUT method is very easy.
  • You do not need to check whether the user has clicked the submit button multiple times or not.
  • It can identify the entity enclosed with the request.

Difference between PUT and POST HTTP Request in Express and Postman

Both PUT and POST are request methods used in the HTTP protocol. In this article, we will introduce the HTTP methods such as PUT and POST in detail, and we will also discuss in detail when to use PUT and when to use POST.

Table of Content

  • PUT method
  • POST Method
  • Difference between PUT and POST method
  • Conclusion

Similar Reads

PUT method

PUT is one of the HTTP request methods and is used to create or update a resource at the specified URI location. In other words, the PUT method essentially saves the body content sent in the request as a resource at the location of the URI path. RESTful APIs often use PUT to update resources....

POST Method

...

Difference between PUT and POST method

POST is one of the HTTP request methods which is used to submit data to be processed to a specified resource. When you make a POST request, you’re typically sending data to the server in the body of the request, and the server processes that data based on the resource identified in the URL....

Conclusion

...

Contact Us