gpt4 book ai didi

jwt - 如何在 Postman 上打开 JWT token 以将其中一个声明值放在变量上

转载 作者:行者123 更新时间:2023-12-04 15:54:13 25 4
gpt4 key购买 nike

要使用 Postman 在我的应用程序上创建一个特定的测试,在登录并获取 JWT token 后,我需要获取一个特定的声明值以在 Postman 上的另一个 POST 的变量中使用。

在不开发 API 的情况下这可能吗?

谢谢

最佳答案

这是一个简单的函数来做到这一点。

let jsonData = pm.response.json();
// use whatever key in the response contains the jwt you want to look into. This example is using access_token
let jwtContents = jwt_decode(jsonData.access_token);

// Now you can set a postman variable with the value of a claim in the JWT
pm.variable.set("someClaim", jwtContents.payload.someClaim);

function jwt_decode(jwt) {
var parts = jwt.split('.'); // header, payload, signature
let tokenContents={};
tokenContents.header = JSON.parse(atob(parts[0]));
tokenContents.payload = JSON.parse(atob(parts[1]));
tokenContents.signature = atob(parts[2]);

// this just lets you see the jwt contents in the postman console.
console.log("Token Contents:\n" + JSON.stringify(tokenContents, null, 2));

return tokenContents;
}

签名位在这个例子中仍然没有用,所以你不能用这个来验证它,但它仍然解决了你的问题。

关于jwt - 如何在 Postman 上打开 JWT token 以将其中一个声明值放在变量上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52607165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com