JIYIK CN >

Current Location:Home > Learning > PROGRAM > Node.js >

HTTP POST request in Node.js

Author:JIYIK Last Updated:2025/04/17 Views:

In this article, we will learn how to use Node.js to make a post request using a third-party package.


HTTP Post Request in Node.js

The HTTP POST method creates or adds resources on the server. The key difference between POST and PUT requests is that new resources are added/created to the server through POST request whereas existing resources are updated/replaced through PUT request.

For example, a browser uses the HTTP POST request method when sending HTML form data to a server or sending data through a jQuery/AJAX request . Unlike GET and HEAD requests, HTTP POST requests can change the state of the server.


Different ways to make HTTP requests in Node.js

Using the Axios library

We can use Axios to send asynchronous HTTP requests to REST endpoints. It becomes easy to perform CRUD operations using Axios.

Install Axios library using the following command.

$ npm i axios

POSTpost() requests are made using the method. post()Axios automatically serializes the JavaScript object into JSON when it is passed as the second argument to the function.

We don't need to serialize the POST body into JSON.

Full source code:

const axios = require('axios');
async function submitRequest() {
  const payload = { title: 'Hello World', body: 'Welcome to Node tutorial' };
  const res = await axios.post('https://jsonplaceholder.typicode.com/posts', payload);
  const data = res.data;
  console.log(data);
}
submitRequest();

In the above example, once the user submits the form, a POST call is sent to the node server with the specified URL (fictitious in this article) and parameters. If the server processes this data without interruption, it returns a success message.

Depending on the output of the server response, you can print a message on the console or notify the user through a message.

Output:

{
  title: 'Hello World',
  body: 'Welcome to Node tutorial',
  id: 101
}

Get the library using Node

We can use the node-fetch library to send asynchronous HTTP requests to a REST endpoint. We can find more information about node-fetch here.

Install the node-get library using the following command.

$ npm i node-fetch

Full source code:

const fetch = require('node-fetch');
async function getTodoData() {
  const payload = { title: 'Hello World', body: 'Welcome to Node tutorial' };
  const response = await fetch('https://jsonplaceholder.typicode.com/posts', {
    method: 'post',
    body: JSON.stringify(payload),
    headers: {'Content-Type': 'application/json'}
  });
  const data = await response.json();
  console.log(data);
}
getTodoData();

The output is as follows:

{
  title: 'Hello World',
  body: 'Welcome to Node tutorial',
  id: 101
}

Using the SuperAgent library

Let's use the SuperAgent library to make an HTTP post request in Node.js. We can find more information about the SuperAgent library here.

Install the superagent library using the following command.

$ npm i superagent

Full source code:

const superagent = require('superagent');
async function getTodoData() {
  const payload = { title: 'Hello World', body: 'Welcome to Node tutorial' };
  const res = await superagent.post('https://jsonplaceholder.typicode.com/posts').send(payload);
  console.log(res.body);
}
getTodoData();

The output is as follows:

{
  title: 'Hello World',
  body: 'Welcome to Node tutorial',
  id: 101
}

Previous: None

Next:Node.js sends files to the client

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Throwing Errors in Node.js

Publish Date:2025/04/17 Views:164 Category:Node.js

This article will explain how to throw errors in Node.js. Throwing Errors in Node.js Errors are statements that do not allow the system to function properly. Errors in Node.Js are handled through exceptions, which are created with the help

Solve the Cannot Find Module error in Node.js

Publish Date:2025/04/17 Views:70 Category:Node.js

In this article, we will learn how to fix the Cannot find module error in Node.js. package.json File Before diving into the solution, we will first try to understand the package.json file and why we need it. The package.json file is the roo

Multithreading in Node.js

Publish Date:2025/04/17 Views:112 Category:Node.js

In Node.js, the term multithreading does not apply because Node.js is designed to run in a single-threaded event loop. However, Node.js is built on top of the JavaScript language, which is single-threaded by default. However, Node.js provid

Using jQuery in Node.js

Publish Date:2025/04/17 Views:51 Category:Node.js

jQuery is a popular JavaScript library that is widely used to build web applications. It provides a rich set of APIs for interacting with the DOM, making HTTP requests, handling events, etc. Node.js is a JavaScript runtime that allows devel

Node.js sends files to the client

Publish Date:2025/04/17 Views:70 Category:Node.js

In this article, we will learn how to send files to the client in Node.js using Express. Sending files using Express in Node.js Express.js or Express is a backend web utility framework for Node.js. Express is a Node.js web application frame

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial