Need a proxy solution? Try ScrapeOps and get 1,000 free requests here, or compare all proxy providers here!
ScrapeOps

Node.js (Got, SuperAgent, Axios & More) - How to Send POST Requests

Node.js (Got, SuperAgent, Axios & More) - How to Send POST Requests
Published Updated

In this guide for The NodeJs Web Scraping Playbook, we will look at how to make POST requests with the NodeJS Got, NodeJS SuperAgent, NodeJS Node-Fetch, NodeJS Axios and NodeJS Request Promise.

In this guide we will walk you through the most common ways of sending POST requests with NodeJS Libraries:

Let's begin...


POST JSON Data Using NodeJS Http Request Libraries

A common scenario for using POST requests is to send JSON data to an API endpoint, etc.

To making POST requests this with:

With NodeJS Got, we simply just need to add the json data to the request using the json parameter within our request options:

JavaScript
import got from 'got';
const url = 'https://httpbin.org/post';
const data = { key: 'value' };
const options = {
json: data
};
got.post(url, options)
.then(response => {
console.log(response.body);
})
.catch(error => {
console.error(error);
});

Here Content-Type header gets automatically set to application/json. And data object is converted into json string before it is sent as POST body.


POST Form Data Using NodeJS Http Request Libraries

Another common use case for using POST requests is to send form data to an endpoint.

To make form data POST requests with:

With Got we simply need to add the data to the request using the form parameter of the request options:

JavaScript
import got from 'got';
const url = 'https://httpbin.org/post';
const data = { key: 'value' };
const options = {
form: data
};
got.post(url, options)
.then(response => {
console.log(response.body);
})
.catch(error => {
console.error(error);
});

Here got automatically sets Content-Type header to application/x-www-form-urlencoded so that the POST body will be picked up as form data. Also data object is converted into query string representation, key=value, before it is sent as POST body.


More Web Scraping Tutorials

So that's how you can send POST requests using NodeJs Got, SuperAgent, Node-Fetch, Axios, and Request Promise, including posting form data and JSON.

If you would like to learn more about Web Scraping, then be sure to check out The Web Scraping Playbook.

Or check out one of our more in-depth guides:

Need help scraping the web?

Then check out ScrapeOps, the complete toolkit for web scraping.

Get 1,000 free API credits