In Axios, you will get a basic API to make a HTTP demand. It is essentially a commitment based HTTP client and you might involve it in vanilla JavaScript and NodeJS. To make a HTTP POST demand in NodeJS with the assistance of Axios, we really want to introduce Axios first. See the beneath order for introducing Axios:
npm install axios // or npm i axios
After installing Axios into our NodeJS program, let’s use it and see the below code example of making an HTTP POST request:
const axios = require('axios'); const data = { name: 'James Mathew', address : 'NY, USA' }; axios.post('http://api.mydomain.com/v1/create', data) .then((res) => { console.log(`Status: ${res.status}`); console.log('Employee Info: ', res.data); }).catch((err) => { console.error(err); }); /* Output: Status: 200 Employee Info: { status: 'success', message: 'Successfully! Record has been added.' } */
You may likewise involve the nonconcurrent capability for making a HTTP POST demand with Axios. How about we see an instance of doing it in the underneath segment:
const axios = require('axios'); const data = { name: 'James Mathew', address : 'NY, USA' }; const createUser = async () => { try { const res = await axios.post('https://mydomain.in/api/users', data); console.log(`Status: ${res.status}`); console.log('Body: ', res.data); } catch (err) { console.error(err); } }; createUser(); /* Output: Status: 201 Body: { name: 'James Mathew', id: 2001, } */