Skip to content

Instantly share code, notes, and snippets.

@christroutner
Created October 21, 2019 15:07
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 christroutner/e259a2a3a041e08440d130fbe1cec92a to your computer and use it in GitHub Desktop.
Save christroutner/e259a2a3a041e08440d130fbe1cec92a to your computer and use it in GitHub Desktop.
JWT Token Demo
/*
A demo of using a JWT token to access a REST API.
*/
const axios = require("axios");
async function runTest() {
try {
for (let i = 0; i < 60; i++) {
const success = await getNetworkInfo();
console.log(`Iteration ${i + 1}, Success.`);
await sleep(1000); // wait 1 second.
}
} catch (err) {
console.log(`error: `, err);
}
}
runTest();
// Promise based sleep.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Returns true if data is successfully retrieved from the demo REST API.
// Throws error if there is any issue.
async function getNetworkInfo() {
try {
const token = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVkYWNkMWUyZmE4MTQ1M2I4NTUxMDc1ZiIsImlhdCI6MTU3MTYwNzg3MiwiZXhwIjoxNTc0MTk5ODcyfQ.e2gmzLaJmKMlbRXqF9RhGs8zT7Xg_pNVQsLPSlET9tM`;
const options = {
method: "GET",
headers: {
"content-type": "application/json",
// authorization: `Token ${token}`
},
url: `https://demo.bchtest.net/v3/control/getNetworkInfo`
};
const result = await axios(options);
// console.log(`info: ${JSON.stringify(result.data,null,2)}`)
return true;
} catch (err) {
throw err;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment