Building Restful APIs using Express: Part 6 – POST Requests

2 min read

In this section, our focus will be on how to respond to HTTP Post requests. In case you missed the tutorial on how to handle http GET requests, here is a link to the tutorial.

We will focus on how to use the http POST request to create a new radio station. Let’s have a look at the code snippet below. Also, the complete index.js file for our online radio application is below the code snippet.

app.use(express.json());

app.post('/api/v1/stations', (req, res) => {
    const station = {
       id: stations.length + 1,
       name: req.body.name
    };
    stations.push(station);

    res.send(station);
});

Code Explanation

Similar to the GET request, we have specified the path to make a post request to, which is the stations resource that holds the collection of radio stations.

Next is the route handler, where we need to read the radio station object that will be in the request body. We use this property to create a new station object, then add the object to the array of radio stations. We manually assigned the id on line 37 by incrementing the array length by one. Assuming we have a database for this project, there will be no need to assign the id manually. It will be generated automatically, once the POST request is successful.

The next property is the name of the radio station, which we read from the request body on line 38. We need to enable json parsing in the body of the request using the code on line 5, in order to read the request body. This is because the feature isn’t enabled by default in Express.

The express.json() method on line 5 returns a middleware, which we are now using in the request processing pipeline.

We use the push method on line 40 to add the station object to the collection of radio stations.

Finally, if the post request is successful, the object we send to the server will be returned back in the response body. This is because the client application might also need to know the value of the object id on the server.

Now that we have successfully handled POST request, how can we test our POST endpoint? As a developer, it’s essential to test all code written before deployment in order to fix bugs. This is where tools such as POSTMAN and RestEasy becomes useful for testing API endpoints. 

Testing HTTP services using POSTMAN

POSTMAN is a chrome extension tool, but now has native apps that is largely acceptable by developers, as it focuses on making API development simple.

In order to install POSTMAN, simply visit the POSTMAN website to download their native apps that runs on Mac, Windows and Linux. Once you download and install the app, it should now be available as part of installed applications on the computer.

Creating a POST Request in PostMan

Firstly, select POST, and enter the url endpoint

http://localhost:3000/api/v1/stations

Secondly, set the body of the request and check the raw radio button. Also, Select the JSON (application/json) option.

Now, we can start parsing a json object into the body of the request as:

{
   "name": "Berlin Community Radio"
}

Finally, hit the send button.

image showing http POST request in POSTMAN

The result of the steps performed above is the server response and the response status. The 200 OK response indicates that the request was handled successfully.

image showing http POST response in POSTMAN

HTTP Put and Delete Requests – Building Restful APIs…

In order to update or delete an information, we need have the object ID. Let's analyze the code block below for handling HTTP Put...
appdevchannel
1 min read

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...
appdevchannel
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...
appdevchannel
1 min read

2 Replies to “Building Restful APIs using Express: Part 6 – POST…”

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.