HTTP Put and Delete Requests – Building Restful APIs using Express: Part 8

1 min read

HTTP Put Request

In order to update an information, for example, in our radio application, we need to implement the HTTP Put request. Let’s analyze the code block below.

app.put('/api/v1/stations/:id', (req, res) => {
   const station = stations.find(s => === 

   if (!station) return res.status(404).send(`radio station with 
   id: ${id} not found`);

   const schema = {
      name: Joi.string().min(5).required()

   const result = Joi.validate(req.body, schema);

   if (result.error) {
   } =;

The code block for the PUT request above, basically look up for a radio station with a given id. If the id doesn’t exist in database, the server will respond with a 404 error, which means that the resource was not found (line 64 – 67).

image showing http put request with an ID not found

In case the id exist, the information in the request body will be validated to make sure it conforms to the requirements defined in the schema on line 69 – 73. If the input is invalid, the server will respond with a 400 error, which indicates a Bad Request (line 77 – 80).

If the radio name is valid, the server will update the radio station with the given id. The new value will also be returned to the client (line 83).

image showing http put request with a found ID

HTTP Delete Request

The HTTP Delete Requests is the D in CRUD operations. Recall that CRUD stands for create, read, update and delete.

Let’s explain the DELETE request, using the code block on line 87 – 98 of our application’s index.js file below.

In order to delete a radio station, we need to find the index of the station in the station array, by using the indexOf method. Line 96 removes an object from the index in line 95 using the javascript splice method.

Finally, the server returns a response to the client on line 97, with the information of the radio station removed.

To confirm if this route is working, let’s make use of the PostMan once again. Let’s delete the radio station with an id of 1.

image of a delete request with a given ID in PostMan

To confirm if we deleted the station with id of 1, let’s make a GET request to retrieve the list of radio stations.

image of GET request in PostMan

Validating POST Request – Building Restful APIs using Express:…

It is important to have a validation mechanism that validates inputs when building web services. Read more as we use the joi package to...
2 min read

Building Restful APIs using Express: Part 6 – POST…

In this section, we will discuss how to use http POST request to create a new radio station. We will test endpoint using the...
2 min read

Restful APIs using Express: Part 5 – GET Request

In this post, I explained the steps required to handle http GET request in Node - Express application. Learn more as we build an...
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss a tutorial from us, get weekly updates in your inbox.