How to display response body using node.js and postman collection?
Last Updated :
20 Dec, 2023
This article includes step by step-by-step procedures to make a request to the API using Axios and display the response to the console as well as a visual view of the response in the Postman application.
What is Postman?
Postman is an API platform that is used to build and use APIs.
- It provides a user-friendly interface for sending and receiving HTTP requests to test, develop, and document APIs.
- Postman offers a range of features and tools that make it easier for developers to work with APIs.
What is Node.js?
Node.js is a cross-platform, open-source server environment that can run on Windows, Linux, Unix, macOS, and more. Node.js is a back-end JavaScript runtime environment, that runs on the V8 JavaScript engine and executes JavaScript code outside a web browser.
Why Display the Response Body?
A response body contains the data that is sent by the server in response to an http request.
- Displaying this information helps in understanding what is the data that is sent by the server and how to use it further as needed.
- We can use this body for debugging, logging or processing the data in our application.
Steps to Display Response Body
Step 1: Import Postman collection into Postman.
Import the postman collection in postman containing requests that we want to make from node.js.
1. Open postman and go to collections tab on the left-hand side. Click on “import” button located at the top-left corner.
2. In the “Import File” option, click on the “Choose Files” button and select the relevant postman collection file.
Javascript
{
"info" : {
"_postman_id" : "unique-collection-id" ,
"name" : "Simple Request Collection" ,
"description" : "A basic Postman collection with a single request" ,
},
"item" : [
{
"name" : "Sample Request" ,
"request" : {
"method" : "GET"
}
}
]
}
|
3. Once the file has been selected, click on the “Import” button to initiate the import process.
4. After the import process is complete, you should see your newly imported collection listed in the Collections tab.
Step 2: Install packages.
Axios is an HTTP client for node.js and the browser. It can run in the browser and Node.js with the same codebase. Install the Axios package using the npm command:
npm install axios
Step 3: Write the node.js code to make an HTTP request and print the response to the console.
Below code snippet uses an axios package to make an http request and prints the response to the console:
Node
const axios = require(‘axios’);
//postman collection URL
const apiUrl = ‘https://api.example.com/endpoint’;
const requestConfig = {
method:’GET’,
headers: {
‘Content-Type’:’application/json’,
},
//data
};
//api request using axios
axios(apiUrl, requestConfig)
.then((response)=> {
console.log(‘Response Body’);
console.log(response.data);
}).catch((err) => {
console.error(‘Error:’, err);
});
Step 4: Run code.
Run NodeJS code using command: –
node index.js
Step 5: Open Postman.
Open the postman application then navigate to collections. Here we have many options at the bottom side including body and header. Inside body there are many options in which we can see the body response as shown below:
Here we can see the body in different forms as shown below:
Pretty form
Raw form
preview (original look of response)
Conclusion
In above article we have learnt what is postman, NodeJS. why we should display a response body. how to display a response body using NodeJS and postman collection.
Share your thoughts in the comments
Please Login to comment...