在邮递员中,我如何获取响应正文并在测试中的新请求中使用它 [英] In postman, how do I take a response body and use it in a new request within Tests

查看:13
本文介绍了在邮递员中,我如何获取响应正文并在测试中的新请求中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Postman 的新手,对 Javascript 完全陌生.

I am new to Postman and completely new to Javascript.

我运行了一个 Post 请求来创建新合同.

I ran a Post request to create a new contract.

请求正文

{
        "progSrvcNm": "009",
        "contractPrtyNm": "PostmanAutomationContract",
        "contractCd": "000",
        "signDt": "2018-01-01",
        "startDt": "2018-01-01",
        "endDt": "2025-01-01",
        "remitTerms": 30
}

并收到一个以新合同号作为响应正文的 ok Response.

and received an ok Response with the new contract number as the response body.

"02974"

我现在想保存响应正文并在 Get 请求中使用它来确认我在 Post 中发送的数据是新合同的 get 中返回的数据.

I now want to save the response body and use it in a Get request to confirm the data I sent in the Post is what is returned in the get for the new contract.

我尝试保存变量并在 Postman 中使用发送请求"片段,但当我运行时,我只得到另一个新合同编号的响应.

I attempted to save the variable and use the 'Send Request' snippet in Postman, and when I run I only get a response of another new contract number created.

let newContractNb = pm.response.json();

pm.sendRequest("http://smat-meddev02/MedeaSMATMEDSQL01AICollationFNGAPI2.AffiliateApi/api/Get/" + newContractNb, function (err, response) {
    console.log(response.json());
});

推荐答案

你可以这样试试,

在第一次 GET 请求时,获取响应正文并将所需数据存储到邮递员环境中,例如 postman.setEnvironmentVariable(key, value) 更具体的做法是

on 1st GET request, grab the response body and store the required data to postman environment like postman.setEnvironmentVariable(key, value) more specifically by doing

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

第二次 GET/POST 请求,要发送 newContractNb,您需要将其设置为 GET/POST 请求的一部分.

on 2nd GET/POST request, To send the newContractNb, you need to set it as part of the GET/POST request.

将其作为参考: http://blog.getpostman.com/2014/01/27/extracting-data-from-responses-and-chaining-requests/

这篇关于在邮递员中,我如何获取响应正文并在测试中的新请求中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆