如何使用Postman中的预请求脚本从另一个请求运行一个请求 [英] How to run one request from another using Pre-request Script in Postman
问题描述
我正在尝试发送一个经过身份验证的请求,只需点击一下邮递员。
I'm trying to send an authenticated request with one click in postman.
所以,我有一个名为Oauth的请求和我正在使用测试将令牌存储在局部变量。
So, I have request named "Oauth" and I'm using Tests to store the token in a local variable.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.access_token);
我现在要做的是自动运行Oauth请求(来自预先请求)脚本)用于任何其他需要持票人令牌的请求。
What I'm trying to do now is that run the Oauth request automatically (from a pre-request script) for any other requests which needs a bearer token.
有没有办法获得一个访问令牌,并通过一个邮递员按钮点击发送一个经过身份验证的请求?
Is there a way to get an access token and send an authenticated request with one postman button click?
推荐答案
我认为还没有办法在预请求脚本中执行此操作,但您可以将其归结为如果您使用变量和测试选项卡,只需单击几下。 邮递员博客上有更全面的说明,但它的要点是:
I don't think there's a way to do this in the pre-request script just yet, but you can get it down to just a few clicks if you use a variable and the Tests tab. There are fuller instructions on the Postman blog, but the gist of it is:
- 正常设置您的身份验证请求。
-
在该请求的测试部分中,将该请求的结果存储在变量中,可能类似于以下内容:
- Set up your authentication request like normal.
In the Tests section of that request, store the result of that request in a variable, possibly something like the following:
var data = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", data.token);
运行身份验证请求 - 您现在应该看到令牌
是为该环境设置的(单击右上角的眼形图标)。
Run the authentication request -- you should now see that token
is set for that environment (click on the eye-shaped icon in the top right).
要刷新令牌,您需要做的就是重新运行身份验证请求。
To refresh the token, all you should need to do is re-run the authentication request.
这篇关于如何使用Postman中的预请求脚本从另一个请求运行一个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!