Skip to content

Instantly share code, notes, and snippets.

@trevorblades
Created May 2, 2019 18:25
Show Gist options
  • Save trevorblades/f183b5ddf9835931b85851a5766d1150 to your computer and use it in GitHub Desktop.
Save trevorblades/f183b5ddf9835931b85851a5766d1150 to your computer and use it in GitHub Desktop.
fornite app notes
<!--
use getUserStats as the form's submit handler
so that users can press "enter" in the text input
-->
<form onsubmit="getUserStats">
<input type="text" name="username">
<button type="submit">
</form>
// define the common parts of the API url as a variable so we don't need to rewrite it every time
const baseUrl = 'https://fortnite-public-api.theapinetwork.com/prod09';
function getUserStats(event) {
// to prevent the regular form submit behavior
event.preventDefault();
// grab the value of the form field named "username"
const username = event.target.username.value;
// start fetching!
fetch(`${baseUrl}/users/id?username=${username}`)
.then(resp => resp.json())
.then(data => fetch(`${baseUrl}/users/public/br_stats_v2?user_id=${data.uid}`))
.then(resp => resp.json())
.then(stats => {
console.log(stats);
})
.catch(error => {
console.log('request failed', error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment