Last active
November 1, 2022 14:45
-
-
Save BarryCarlyon/b16e5e3771e4fb51d1818ef86b4fdfaa to your computer and use it in GitHub Desktop.
Javascript Fetch Example. That does JSON and preserves statuscode
This file contains hidden or 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
let url = new URL('some url'); | |
let params = [ | |
[ 'param_1', 'value' ], | |
[ 'param_2', 'value' ] | |
]; | |
url.search = new URLSearchParams(params).toString(); | |
fetch( | |
url, | |
{ | |
method: 'GET', | |
headers: { | |
'Accept': 'application/json' | |
} | |
} | |
) | |
.then(r => r.json().then(data => ({ status: r.status, headers: r.headers, body: data }))) | |
.then((r) => { | |
if (r.status == 200) { | |
console.log('Got Data', r.body); | |
} else { | |
console.log('Error', r.status, r.body); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment