Created
October 21, 2019 15:07
-
-
Save christroutner/e259a2a3a041e08440d130fbe1cec92a to your computer and use it in GitHub Desktop.
JWT Token Demo
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
/* | |
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