Node.js (Got, SuperAgent, Axios & More) - How to Send POST Requests
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:
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.
With NodeJS SuperAgent, we simply just need to call request.post method with url and follow that with a call to send method prividing the request body as an argument:
const request = require("superagent");
const url = 'https://httpbin.org/post';const body = { key: 'value' };
request.post(url) .send(body) .set('Content-Type', 'application/json') .then(response => { console.log(response.body); }) .catch(error => { console.error(error); });
With Node-Fetch, we simply just need to add the data to the request using the body parameter within our fetch options:
import fetch from 'node-fetch';const url = 'https://httpbin.org/post';const body = { key: 'value' }const options = { method: 'POST', body: body, headers: { 'Content-Type': 'application/json' }};
fetch(url, options) .then(response => { return response.json() }) .then(data => { console.log(data) }) .catch(error => { console.error(error); });
With NodeJS Axios, we simply just need to call axios.post method with data as the second argument:
const axios = require('axios');
const uri = 'https://httpbin.org/post'const data = { key: 'value' }const options = { headers: { 'Content-Type': 'application/json' }}
axios.post(uri, data, options) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });
With NodeJS Request Promise, we simply just need to add the data to the request using the body parameter within our request options:
const request = require('request-promise');
const options = { method: 'POST', uri: 'https://httpbin.org/post', body: { key: 'value' }, json: true, headers: { 'Content-Type': 'application/json' }};
request(options) .then(response => { console.log(response); }) .catch(error => { console.error(error); });
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:
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.
With SuperAgent we simply need to set the data to the request by calling the send method:
const querystring = require('querystring');const request = require("superagent");
const url = 'https://httpbin.org/post';const data = { key: 'value' }const body = querystring.stringify(data);
request.post(url) .send(body) .set('Content-Type', 'application/x-www-form-urlencoded') .then(response => { console.log(response.body) }) .catch(error => { console.error(error); });
Here we set the Content-Type header to application/x-www-form-urlencoded so that the body will be picked up as form data. Also note that we have set body to a string representation of data calculated using querystring.stringify function.
With Node-Fetch we simply need to add the data to the request using the body parameter of the POST request:
import fetch from 'node-fetch';import querystring from 'querystring';const url = 'https://httpbin.org/post'const data = { key: 'value' }const options = { method: 'POST', body: querystring.stringify(data), headers: { 'Content-Type': 'application/x-www-form-urlencoded' }};
fetch(url, options) .then(response => { return response.json() }) .then(data => { console.log(data) }) .catch(error => { console.error(error); });
Here we set the Content-Type header to application/x-www-form-urlencoded so that the body will be picked up as form data. Also note that we have set body to a string representation of data calculated using querystring.stringify function.
With Axios we simply need to call axios.post method with form data as the second argument.
const axios = require('axios');
const uri = 'https://httpbin.org/post'const data = `key1=value1&key2=value2`const options = { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}
axios.post(uri, data, options) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); });
Here Content-Type header in the third options argument is set to application/x-www-form-urlencoded so that the body will be picked up as form data.
With Request-Promise we simply need to add the data to the request using the data parameter of the POST request:
const request = require('request-promise');
const options = { method: 'POST', uri: 'https://httpbin.org/post', body: { key: 'value' }, json: true, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }};
request(options) .then(response => { console.log(response); }) .catch(error => { console.error(error); });
Here we set the Content-Type header to application/x-www-form-urlencoded so that the body will be picked up as form data.
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:
Then check out ScrapeOps, the complete toolkit for web scraping.