Axios is a promise-based HTTP client for JavaScript that can be used in front-end applications and Node.js backends.
A POST request can be made using Axios to “post” data to an endpoint. This endpoint may then use this POST request to perform a certain task or trigger an event. The HTTP post request is performed by calling axios.post()
.
This method requires two parameters. First, it needs the URI of the service endpoint. Second, an object which contains the properties that we want to send to our server should be passed to it.
Passing a data object as a parameter to the post
method is optional; in this way, a post method is very similar to the get
method.
The following code shows a sample POST request without any data object passed to it. The code also demonstrates how the POST request can use async/await.
const axios = require('axios')
const res = await axios.post('https:sample-endpoint.com/data')
To send a data object with the POST request, simply pass the object as the second argument in the post
method. The following code demonstrates this using a promise-based code:
const axios = require('axios')
axios.post('https:sample-endpoint.com/user', {
Name: 'Fred',
Age: '23'
})
.then(function (response) {
console.log(response);
})