Axios is a promise-based HTTP client for JavaScript.
It is used to send asynchronous HTTP request to REST endpoints.
It can be used with plain javascript and popular front-end frameworks like React, Angular and VueJS.
Below are some frequently used requests for performing any CRUD operations.
GET Request
axios.get('https://reqres.in/api/users?delay=2') .then(response => { console.log(response.data); }) .catch(error => { // handle error console.log("This is error: " + error.response); }) .finally(function () { // always executed });
POST Request
axios.post('https://reqres.in/api/users', { "name":"aman", "job":"it" }) .then(response => { console.log(response.data); }) .catch(error => { console.log(error.response); }) .finally(function(){ //always executed });
PUT request
axios.put('https://reqres.in/api/users/2', { "name":"Raju", "job":"Dev Ops", }) .then(response => { console.log(response.data); }) .catch(error => { console.log(error.response); }) .finally(function(){ //always executed });
DELETE Request
axios.delete('https://reqres.in/api/users?page=2') .then(response => { console.log(response.data); }) .catch(error => { console.log(error.response); }) .finally(function(){ //always executed });
Post Request with Authorization header
Same way for other requests like GET, PUT, DELETE etc..
axios.post('url', { "data":"somedata" }, { headers:{'Authorization': 'Token'} }) .then(response => { console.log(response.data); }) .catch(error => { console.log(error.response); }) .finally(function(){ //always executed });