Created
October 12, 2022 12:46
-
-
Save pforret/8095687beb2ed87850734ecf47ea4d77 to your computer and use it in GitHub Desktop.
checklyhq snippet to get a JWT token with username/password
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
// this snippet is saved under https://app.checklyhq.com/snippets | |
var axios = require('axios'); | |
// SERVICE_EMAIL/SERVICE_PASSWORD/SERVICE_LOGIN_URL are saved in the Environment Variables | |
// https://app.checklyhq.com/environment-variables | |
const email = process.env.SERVICE_EMAIL | |
const password = process.env.SERVICE_PASSWORD | |
const url = process.env.SERVICE_LOGIN_URL | |
var data = '{ "email": "' + email + '", "password": "' + password + '" }'; | |
var config = { | |
method: 'post', | |
url: url', | |
headers: { | |
'Accept': 'application/json', | |
'X-Requested-With': 'XMLHttpRequest', | |
'Content-Type': 'application/json;charset=UTF-8' | |
}, | |
data: data | |
}; | |
var token=""; | |
var result = await axios(config) | |
.then(function (response) { | |
// token is returned in .data.token | |
token = response.data.data.token; | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}); | |
// now use Token as Bearer Token | |
request.headers['Authorization'] = `Bearer ${token}` | |
// Now you can use this snippet every time you need a JWT token before doing an API check |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment