Postman scripting: how to decode token
Asked Answered
P

1

13

I'm using postman with scripting.

  1. First, I perform a request to retrieve a oauth token.

  2. Then, inside the 'Test' tab, I'm using postman scripting to use the received token to set a global (postman) variable.

Additionally, I would like to decode the token, because I want to use information inside the token to set them as variables. The token payload is base 64 url encoded.

How do I do that?

enter image description here

enter image description here

Polyploid answered 6/11, 2018 at 8:42 Comment(0)
P
17

I found this piece of code on the net. It uses atob sandboxed script to decode base 64 encoded payload

const jsonData = JSON.parse(responseBody);
const payload = jsonData.id_token.split('.')[1];  // Assuming the JWT is in id_token
const parsed = JSON.parse(atob(payload));
pm.environment.set('user_id', parsed.user_id); // Assuming user_id is in the payload
Polyploid answered 12/11, 2018 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.