Skip to content

Instantly share code, notes, and snippets.

@Ranner198
Created February 21, 2019 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ranner198/0464d447d9f9bf148e00ba5e593e785a to your computer and use it in GitHub Desktop.
Save Ranner198/0464d447d9f9bf148e00ba5e593e785a to your computer and use it in GitHub Desktop.
//This is the npm package that will allow you to by-pass the 301 redirect error
const https = require('follow-redirects').https;
//This is your URL link to query the system and your username
var url = 'https://public-api.tracker.gg/apex/v1/standard/profile/<YourSystem>/<YourName>';
//This is our GET-request module
https.get(url, {
headers: {
"TRN-Api-Key" : "<YourApiKeyHere>" //<----This is where you will pass your API key to
}
}, function (res) {
//On the server response we run this function
//We create a Data link to save the redirect URL links so we can recursivly pass through the URL redirect until we hit the endpoint
var data = '';
res.on('data', function (chunk) {
data += chunk.toString();
});
//On the endpoint we pull our data
res.on('end', function () { //Reached Endpoint of API
//Save a temp variable to store the JSON data into
var json;
//We will do a try-catch to check if the JSON is valid
try {
//If it is we parse the data to make it 'pretty printed' so we can read it
json = JSON.parse(data);
//We then want to print it out to the console
console.log(json.data);
}
catch (e) {
// Not json or JSON formatting issue print it here
console.log("this is an error: " + e);
}
});
}).on('error', function (err) {
//If there was a big error while accessing the server display it here
console.error(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment