How to make an Axios POST request
Axios is a promise-based HTTP client for node.js which can be deployed both frontend and backend.
To perform a POST request using Axios and thus send data to a particular end-point use the axios.post()
function.
Basic POST Request
Let's import the axios
package and perform a basic POST request to an end-point, which doesn't send data.
const axios = require('axios');
const res = await axios.post('https://example.com/api');
Send Data with Axios Post Request
To send data in a POST request with Axios, pass it as the second argument of the axio.post()
function. In the example below, we'll send an object then catch the response with .then()
.
const axios = require('axios')
axios.post('https://example.com/api', {
Name: 'John',
LastName: 'Smith'
})
.then(function (response) {
console.log(response);
});
http
request