【问题标题】:How to run one request from another using Pre-request Script in Postman如何使用 Postman 中的预请求脚本从另一个请求运行一个请求
【发布时间】:2017-01-25 20:55:46
【问题描述】:

我正在尝试在邮递员中一键发送经过身份验证的请求。

所以,我在一个局部变量中有一个名为“Oauth”和I'm using Tests to store the token 的请求。

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.access_token);

我现在要做的是为任何其他需要不记名令牌的请求自动运行 Oauth 请求(来自预请求脚本)。

有没有办法通过单击邮递员按钮来获取访问令牌并发送经过身份验证的请求?

【问题讨论】:

    标签: javascript postman


    【解决方案1】:

    您不能从Pre-request Script 部分发送另一个请求,但实际上,可以链接请求并一个接一个地运行。

    您将您的请求收集到 collection 和 run itCollection Runner

    要查看请求结果,您可以关注other answer

    【讨论】:

    【解决方案2】:

    注意:现在一种在预请求脚本中执行此操作的方法,请参阅the other answers。我会为后代保留这个答案,但让每个人都知道:)

    我认为目前还没有办法在预请求脚本中执行此操作,但如果您使用变量和“测试”选项卡,只需单击几下即可完成此操作。 There are fuller instructions on the Postman blog,但它的要点是:

    1. 像往常一样设置您的身份验证请求。
    2. 在该请求的测试部分,将该请求的结果存储在一个变量中,可能类似于以下内容:

      var data = JSON.parse(responseBody);
      postman.setEnvironmentVariable("token", data.token);
      
    3. 运行身份验证请求——您现在应该看到为该环境设置了token(单击右上角的眼睛形图标)。

    4. 将您的数据请求设置为使用 {{token}} 之前粘贴到不记名令牌中的任何位置。
    5. 运行您的数据请求 - 现在应该已正确验证。

    要刷新令牌,您需要做的就是重新运行身份验证请求。

    【讨论】:

    • 我意识到这就是问题首先要做的事情,但是很容易搜索标题而不知道 XD
    • 感谢您让我的思路正确)现在您将得到Identifier 'data' has already been declared 运行上面的代码。要修复它,我们只需要重命名变量 data -> body
    【解决方案3】:

    有点晚了,但对于看到这篇文章的其他人,现在可以从Pre-request Script 部分发送另一个请求。可以在此处找到一些示例:https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

    【讨论】:

      【解决方案4】:

      正如 KBusc 所提到的并从这些示例中得到启发,您可以通过设置如下所示的预请求脚本来实现您的目标:

      pm.sendRequest({
          url: pm.environment.get("token_url"),
          method: 'GET',
          header: {
              'Authorization': 'Basic xxxxxxxxxx==',
          }
      }, function (err, res) {
          pm.environment.set("access_token", res.json().token);
      });
      

      然后您只需将{{access_token}} 引用为任何其他环境变量。

      【讨论】:

      • 如何从我想要的任何请求的预请求中运行来自我的工作区的请求之一,例如仅按名称?
      • 不确定我是否理解您的问题 Dmitry 但我怀疑它与原始问题并不严格相关,并且可能与从那时起添加到 Postman 的新功能有关。也许打开一个新问题会更多合适吗?
      【解决方案5】:

      您可以向集合中添加一个预请求脚本,该脚本将在每个 Postman 请求之前执行。例如,我使用以下内容从 Apigee 返回访问令牌

      const echoPostRequest = {
        url: client_credentials_url,
        method: 'POST',
        header: 
            'Authorization: Basic *Basic Authentication string*'
      
      };
      
      var getToken = true;
      
      if (!pm.environment.get('token'))
      
      {
          console.log('Token  missing')
      
      }
      else 
      {
      
          console.log('Token all good');
      }
      
      if (getToken === true) {
          pm.sendRequest(echoPostRequest, function (err, res) {
          console.log(err ? err : res.json());
              if (err === null) {
                  console.log('Saving the token');
                  console.log(res);
                  var responseJson = res.json();
                  console.log(responseJson.access_token);
                  pm.environment.set('token', responseJson.access_token)
      
      
              }
          });
      }
      

      【讨论】:

        【解决方案6】:

        我尝试了多种解决方案,以下解决方案与您解析请求 1 的响应并将任何变量传递给第二个请求参数时有关。 (在此示例中,变量是姓氏。)

        注意:- 数据和用户是 JSON 对象。``

        postman.clearGlobalVariable("variable_key");
        postman.clearEnvironmentVariable("variable_key");
        tests["Body matches string"] = responseBody.has("enter the match string ");
         var jsonData = JSON.parse(responseBody);
          var result = jsonData.data;
          var lastName = result.user.lastName;
        tests["Body matches lastName "] = responseBody.has(lastName);
        tests["print  matches lastName " + lastName ] = lastName;
        

        【讨论】:

          【解决方案7】:

          你可以使用邮递员的函数sendRequest。

          这里有一些例子。

          https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

          【讨论】:

            【解决方案8】:

            我想到了同样的问题,基本上是“如何在不使用pm.sendRequest(reqConfObj) 构建该请求的情况下运行另一个请求的测试或预请求脚本选项卡中已经存在的另一个请求?”,然后我找到了@来自 this Postman discussion 的 987654324@ 方法将引导您到 this postman documentation page 了解构建请求工作流。

            但问题是,如果您没有运行文件夹或集合,postman.setNextRequest() 方法将不会运行,因此只需点击包含您的脚本的请求的“发送”按钮将不起作用。

            我还想提请您注意一些事情:

            • 前置词,它是“邮递员”而不是“下午”。
            • postman.setNextRequest() 将始终最后运行,即使您已将其写入脚本顶部。脚本中的其他代码将运行,然后 postman.setNextRequest 将初始化。
            • 如果您想停止请求流,您可以简单地postman.setNextRequest(null)

            我会鼓励所有使用 Postman 的人查看上面提到的链接,我相信这是一个很棒的功能,每个人都应该尝试一下! :)

            【讨论】:

              猜你喜欢
              • 2018-06-14
              • 2018-02-28
              • 1970-01-01
              • 1970-01-01
              • 2021-05-11
              • 2018-12-10
              • 2018-11-15
              • 2019-07-18
              • 2021-03-01
              相关资源
              最近更新 更多