Skip to content

Instantly share code, notes, and snippets.

View sabesansathananthan's full-sized avatar
:octocat:
Work

Sathananthan Sabesan sabesansathananthan

:octocat:
Work
View GitHub Profile
const controller = new AbortController();
const signal = controller.signal;
const options = {
method: 'POST',
signal: signal,
body: JSON.stringify({
firstName: 'Sabesan',
lastName: 'Sathananthan'
})
};
// request interceptors
axios.interceptors.request.use((config)=>{
console.log('Request was sent');
return config;
})
// response interceptors
axios.interceptors.response.use((response) => {
// do an operation on response
return response;
fetch = (originalFetch => {
return (...arguments) => {
const result = originalFetch.apply(this, arguments);
return result.then(console.log('Request was sent'));
};
})(fetch);
fetch('url')
.then(response => response.json())
.then(data => {
const config = {
onUploadProgress: event => console.log(event.loaded)
};
axios.put("/api", data, config);
loadProgressBar();
function downloadFile(url) {
axios.get(url, {responseType: 'blob'})
.then(response => {
const reader = new window.FileReader();
reader.readAsDataURL(response.data);
reader.onload = () => {
document.getElementById('img').setAttribute('src', reader.result);
}
// original code: https://github.com/AnthumChris/fetch-progress-indicators
const element = document.getElementById('progress');
fetch('url')
.then(response => {
if (!response.ok) {
throw Error(response.status+' '+response.statusText)
}
axios.get('url')
.then((response)=> console.log(response))
.catch((error)=>{
if(error.response){
// When response status code is out of 2xxx range
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request){
//When no response was received after request was made
const checkError = response => {
if (!response.ok) throw Error(response.statusText);
return response.json();
};
fetch("url")
.then(checkError)
.then(data => console.log(data))
.catch(error => console.log("error", error));
fetch('url')
.then((response)=>{
if(!response.ok){
throw Error (response.statusText);
}
return response.json();
})
.then((data)=>console.log(data))
.catch((error)=>console.log(error))
@sabesansathananthan
sabesansathananthan / AxiosJson.js
Last active February 9, 2021 05:10
Axios Vs Fetch
axios.get('url')
.then((response)=>console.log(response))
.catch((error)=>console.log(error))