Last active
June 24, 2021 13:31
-
-
Save NAbdulla1/6a7282586b5bf00a5b1ff44eb925600c to your computer and use it in GitHub Desktop.
JS http rest client fetch and axios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
axios.get('http://httpbin.org/status/400') | |
.then(function (success) { | |
// response code is 2xx | |
console.log('success:', success.data); | |
console.log('success:', success.status); | |
console.log('success:', success.headers); | |
}) | |
.catch(function (error) { | |
//render the error for better UX | |
if (error.response) { | |
// The request was made and the server responded with a status code | |
// that falls out of the range of 2xx | |
console.log('error response:', error.response.data); | |
console.log('error response:', error.response.status); | |
console.log('error response:', error.response.headers); | |
} else if (error.request) { | |
// The request was made but no response was received | |
// Can be network failure | |
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of | |
// http.ClientRequest in node.js | |
console.log('error request:', error.request); | |
} else { | |
// Something happened in setting up the request that triggered an Error | |
console.log('Error', error.message); | |
} | |
console.log(error.config); | |
}); | |
//using try/catch and async/await | |
async function test() { | |
try { | |
let success = await axios.get('http://httpbin.org/status/400'); | |
console.log('success:', success.data); | |
console.log('success:', success.status); | |
console.log('success:', success.headers); | |
} catch (error) { | |
//render the error for better UX | |
if (error.response) { | |
// The request was made and the server responded with a status code | |
// that falls out of the range of 2xx | |
console.log('error response:', error.response.data); | |
console.log('error response:', error.response.status); | |
console.log('error response:', error.response.headers); | |
} else if (error.request) { | |
// The request was made but no response was received | |
// Can be network failure | |
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of | |
// http.ClientRequest in node.js | |
console.log('error request:', error.request); | |
} else { | |
// Something happened in setting up the request that triggered an Error | |
console.log('Error', error.message); | |
} | |
console.log(error.config); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('http://httpbin.org/status/500') | |
.then(function (success) { | |
if (!success.ok) throw success;// response code is not 2xx | |
// response code is 2xx | |
success.json() | |
.then(function (json) { | |
console.log(json) | |
}) | |
.catch(function (err) { | |
console.log('no data', err.message) | |
}); | |
console.log('success:', success.status); | |
}) | |
.catch(function (error) { | |
//render the error for better UX | |
if ('status' in error) { | |
// The request was made and the server responded with a status code | |
// that falls out of the range of 2xx | |
error.json() | |
.then(function (json) { | |
console.log(json) | |
}) | |
.catch(function (err) { | |
console.log('no data', err.message) | |
}); | |
console.log('error response:', error.status); | |
} else { | |
// The request was made but no response was received | |
// Can be network failure | |
console.log('error request:', error.message); | |
} | |
}); | |
//using try/catch and async/await | |
async function test() { | |
try { | |
let success = await fetch('http://httpbin.org/status/500'); | |
if (!success.ok) throw success;// response code is not 2xx | |
// response code is 2xx | |
try { | |
let json = await success.json(); | |
console.log(json); | |
} catch (err) { | |
console.log('no data', err.message) | |
} | |
console.log('success:', success.status); | |
} catch (error) { | |
//render the error for better UX | |
if ('status' in error) { | |
// The request was made and the server responded with a status code | |
// that falls out of the range of 2xx | |
try { | |
let json = await error.json(); | |
console.log(json); | |
} catch (err) { | |
console.log('no data', err.message) | |
} | |
console.log('error response:', error.status); | |
} else { | |
// The request was made but no response was received | |
// Can be network failure | |
console.log('error request:', error.message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment