NodeJs Http Request Libraries: How to Send POST Requests
- Got
- SuperAgent
- Node-Fetch
- Axios
- Request Promise
To send POST requests with Got, simply call the got.post method with url as the first argument and request options as the second argument. Inside options object, you need to add the POST body string and headers object. headers is where you define http headers like Content-Type.
import got from 'got';
const url = 'https://httpbin.org/post';
const options= {
body: 'Hello world!',
headers: {
'Content-Type': 'text/plain'
}
};
got.post(url, options)
.then(response => {
console.log(response.body);
})
.catch(error => {
console.log(error);
})
To send POST requests with SuperAgent, first create a new post request by calling request.post method with url as an argument. Then set the post body by using send method of the request. To configure a request header, just chain set method with previous call to send method and provide the header name (Content-Type) and value (application/json):
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);
});
To send POST requests with Node-Fetch call the fetch method with url as the first argument and options as the second argument. Inside options object, you need to add the POST bodystring and headers object. headers is where you define http headers like Content-Type.
import fetch from 'node-fetch';
const url = 'https://httpbin.org/post';
const options = {
method: 'POST',
body: { key: 'value' },
headers: {
'Content-Type': 'application/json'
}
};
fetch(url, options)
.then(response => {
return response.json()
})
.then(data => {
console.log(data)
})
.catch(error => {
console.error(error);
});
To send POST requests with NodeJS Axios call the axios.post method with uri as the first argument and POST body as the second argument. The third argument is the options object and here you can set Content-Type using the headers parameter.
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);
});
To send POST requests with NodeJS Request Promise use the request method and add the POST body and Content-Type using the body and headers parameters.
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);
});
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:
- POST JSON Data Using NodeJS Http Request Libraries
- POST Form Data Using NodeJS Http Request Libraries
Let's begin...
Need help scraping the web?
Then check out ScrapeOps, the complete toolkit for web scraping.
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:
- Got
- SuperAgent
- Node-Fetch
- Axios
- Request Promise
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:
- Got
- SuperAgent
- Node-Fetch
- Axios
- Request Promise
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: