Skip to content

Instantly share code, notes, and snippets.

@sdsanchezm
Last active July 13, 2023 15:38
Show Gist options
  • Save sdsanchezm/248ad4fdbfdf901d1e8cee3bc27fad89 to your computer and use it in GitHub Desktop.
Save sdsanchezm/248ad4fdbfdf901d1e8cee3bc27fad89 to your computer and use it in GitHub Desktop.
Axios Helper for React.js
// const axios = require('axios');
import axios from 'axios';
const BASE_URL = `https://somewhere123.website`;
function getData1(url_path){
// Make a request for a user with a given ID
// axios.get('/user?ID=12345')
const URL1 = `${BASE_URL}${url_path}`;
axios.get(URL1)
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
console.log('this is finally, from getData1');
});
}
function getData2(url_path){
const URL1 = `${BASE_URL}${url_path}`;
// axios.get('/user', {
axios.get(URL1, {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
// always executed
console.log('this is finally from getdata2');
});
}
async function getData3(url_path) {
try {
const URL1 = `${BASE_URL}${url_path}`;
const response = await axios.get(URL1);
console.log(response);
return response.data;
} catch (error) {
console.error(error);
}
}
export { getData1, getData2, getData3 };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment