Created
May 30, 2024 01:32
-
-
Save shashank-shekhar/70f1355048864b3080c6249d64cd971f to your computer and use it in GitHub Desktop.
Postman script to exract access_token from response and save it into a variable
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
var response = JSON.parse(pm.response.text()); | |
var jwt = response.access_token; | |
pm.collectionVariables.set("access_token", jwt); | |
var cryptoJS = require('crypto-js') | |
function base64UrlDecode(base64Url) { | |
let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | |
switch (base64.length % 4) { | |
case 2: base64 += '=='; break; | |
case 3: base64 += '='; break; | |
} | |
return cryptoJS.enc.Base64.parse(base64).toString(cryptoJS.enc.Utf8); | |
} | |
function decodeJWT(token) { | |
try { | |
const parts = token.split('.'); | |
if (parts.length !== 3) { | |
throw new Error('Invalid token'); | |
} | |
const header = JSON.parse(base64UrlDecode(parts[0])); | |
const payload = JSON.parse(base64UrlDecode(parts[1])); | |
const signature = parts[2]; | |
console.log('Header:', JSON.stringify(header, null, 2)); | |
console.log('Payload:', JSON.stringify(payload, null, 2)); | |
console.log('Signature:', signature); | |
} catch (error) { | |
console.error('Error decoding token:', error); | |
} | |
} | |
decodeJWT(jwt); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment