Axios cheat sheet

September 18th, 20181 min read

Using axios custom instance

import axios from 'axios';

const instance = axios.create({    
     baseURL: 'http://localhost:3000'
});

export default instance; 

GET request

const getSomeData = async () => await axios.get(`/tasks`);
getSomeData()
    .then(response => console.log(response.data))
    .catch(err => console.log(err)); 

POST request

const postSomeData = async objparam => await axios.post(`/tasks`, objparam);
postSomeData({
        firstName: 'Fred',
        lastName: 'Flintstone'
    })
    .then(response => console.log(response.data))
    .catch(err => console.log(err));

PUT request

const putSomeData = async (id, objparam) => await axios.put(`/tasks/${id}`, objparam);
putSomeData(3, {
        firstName: 'Thomas',
        lastName: 'Bernardes'
    })
    .then(response => console.log(response.data))
    .catch(err => console.log(err));

DELETE request

const deleteSomeData = async id => await axios.delete(`/tasks/${id}`);
deleteSomeData(2)
    .then(response => console.log(response.data))
    .catch(err => console.log(err));