Node

Node Make HTTP POST Request with HTTP Headers Tutorial

In this short guide, we will show you how to make HTTP Post request in Node. Also, how to add additional information with http post request using http headers.

HTTP Requests are great; they talk and talk for the sake of communication with another server. Generically, HTTP requests are made to fetch the resources from other locations or servers.

We will not talk about the types of HTTP requests because there are so many. However, we will talk about the HTTP Post requests and HTTP Headers.

We are pretty much sure most of you must know how to make the basic HTTP post request in Node. But we are talking about things from HTTP Headers outlook.

We all know HTTP requests add, fetch, update, delete data, but what do HTTP headers do.

HTTP headers allow the client and the server to pass extra information with an HTTP request or response.

An HTTP header consists of its case-insensitive name followed by a colon (:), then by its value. Whitespace before the value is ignored.

How to Make HTTP POST Request with HTTP Headers in Node

  • Step 1: Set Up Node App
  • Step 2: Install Axios Package
  • Step 3: Create Script File
  • Step 4: Add Headers in POST Request
  • Step 5: Run HTTP Script

Set Up Node App

In order to set up a new app, we first need a folder where we can put files and modules.

Here is the command that can help you generate the blank new folder.

mkdir node-dune

As soon as you created the folder, get into it using below command.

cd node-dune

The core of node app is a package.json file, this file holds the metadata that powers the node project.

Here is the command that can help you generate the package.json file.

npm init

Install Axios Package

In order to handle the HTTP request make sure to install axios package in the node app using the below command.

npm install axios

Create Script File

To send the http post request with http headers, we need a file, so create app.js file.

In order to execute this script, we need to add the script with the scripts property in the package.json file.

...
...

"scripts": {
    "start": "node server.js"
  },
...
...

Add Headers in POST Request

Head over to app.js file, in this file we have to write some code that will communicate with server.

We have used the json server to make the dummy server; if you have your own API, you can use that.

const axios = require('axios')

const DATA = {
  name: 'John Doe',
  email: 'john@doe.com',
}

const HEADER = {
  headers: { Accept: 'application/json' },
}

axios
  .post('http://localhost:3000/users', DATA, HEADER)
  .then((response) => {
    if (response.status === 201) {
      console.log('Req body:', response.data)
      console.log('Req header :', response.headers)
    }
  })
  .catch((e) => {
    console.error(e)
  })

Run HTTP Script

Now, go to terminal open the console on the console type the given command and press enter.

node start

On the successful post request the given output will appear on your screen.

Req body: { name: 'John Doe', email: 'john@doe.com', id: 2 }
Req header : {
  'x-powered-by': 'Express',
  vary: 'Origin, X-HTTP-Method-Override, Accept-Encoding',
  'access-control-allow-credentials': 'true',
  'cache-control': 'no-cache',
  pragma: 'no-cache',
  expires: '-1',
  'access-control-expose-headers': 'Location',
  location: 'http://localhost:3000/users/2',
  'x-content-type-options': 'nosniff',
  'content-type': 'application/json; charset=utf-8',
  'content-length': '62',
  etag: 'W/"3e-8CwdxGYDiusLoZr41H06zDh6yUk"',
  date: 'Wed, 09 Feb 2022 13:50:16 GMT',
  connection: 'close'
}

Summary

In this Node HTTP Headers and Post request example, we have comprehended how to set up a basic node app and Axios package to handle HTTP request.

Most importantly, we have discovered how to diligently make less cumbersome HTTP post request using the Axios library. Also, how to add HTTP headers in an HTTP request in the Node js application from absolute precision.

Digamber

I am Digamber, a full-stack developer and fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.

Recent Posts

How to Add Active Class in Map Items in React Js

In this quick tutorial, we will show you how to quite easily add the active…

2 days ago

How to Create and Use Redux Store in React Js

In this tutorial, we will learn how to create a Redux Store in React application.…

5 days ago

How to Build Dynamic Load More Feature in React Js

This detailed guide will cover how to create the Load More button and display data…

1 week ago

How to Integrate Redux Persist to React Redux Store

In this tutorial, we will step by step learn how to configure redux persist in…

2 weeks ago

React Redux Save Data in Local Storage with Persist Tutorial

In this comprehensive tutorial, we will learn how to persist redux store's states in React…

2 weeks ago

React Js Tailwind CSS Add Light and Dark Mode Toggler Tutorial

In this guide, we are going to learn how to add dark mode and light…

2 weeks ago