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

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

问题描述

我是Postman的新手,也是Java语言的新手。

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
}

并以新合同编号作为响应正文收到了不错的响应。

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

"02974"

我现在要保存响应正文并将其用于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);

第2个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/

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

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