Node JS Http Request with Headers Example
In this article, we will explain to you how to make http request with headers in node js(Node JS Http Request with Headers Example). so we will give you a simple example of how to set header in node js request(nodejs https request example).
sometimes, we need to set headers in HTTP request API data. such as API key passed in an HTTP header and Content-Type.
There are two ways we can easily add a header with a request using the npm Axios package and the npm request package.
In this example, we will create an HTTP post request with headers example. so you can see both examples nodejs axios http request with headers and npm request module get example.
Node Js Application Setup
First, we will open the command prompt and create the application in our directory. for this, you can follow the below command.
1 2 3 | mkdir my_node_app cd my_node_app npm init |
Install axios NPM Package
After the done setup node js application, we will install the Axios npm package. for that, you can see the following command to install Axios via NPM.
1 | npm install axios --save |
Request npm Set Headers
In this step, We will create the server.js file. after then install the express npm package and set up our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | var express=require('express'); const axios = require('axios'); var app=express(); app.get('/add_product',function(req,res){ const article = { title: 'test product', price: 13.5, description: 'lorem ipsum set', image: 'https://i.pravatar.cc', category: 'electronic' }; const options = { headers: {'Accept': 'application/json'} }; axios.post("https://fakestoreapi.com/products", article, options).then(function(response) { console.log(response.data) }).catch(function(error) { console.log(error) }); }); app.listen(3000,function(){ console.log("Express Started on Port 3000"); }); |
Install request NPM Package
After the done setup node js application, we will install the request npm package. for that, you can see the following command to install request via NPM.
1 | npm install request --save |
Axios HTTP POST Request With Headers
In this step, We will create the server.js file. after then install the express npm package and set up our application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | const request = require('request'); var app=express(); app.get('/add_product',function(req,res){ const options = { url: 'https://fakestoreapi.com/products', json: true, body: { title: 'test product', price: 13.5, description: 'lorem ipsum set', image: 'https://i.pravatar.cc', category: 'electronic' }, headers: {'Accept': 'application/json'} }; request.post(options, (err, res, body) => { if (err) { return console.log(err); } console.log(res.data); console.log(body); }); }); app.listen(3000,function(){ console.log("Express Started on Port 3000"); }); |
Run Node js Application
we will run the node js application using the below command. so you can following the below command.
1 | node server.js |
Now you can run the example using the below Url in the browser.
1 | http://localhost:3000/update_product |