如何在 Postman 中验证响应?如何在、Postman

2023-09-07 10:24:45 作者:你是我不爱别人的原因

我正在尝试验证响应正文,包括邮递员中的错误.如何验证下面的响应和文本?

I am trying to validate response body including errors in postman. How can I validate the response and text below?

{
    "responseHeader": {
        "publisherId": "12345",
        "responseId": "abbcb15d79d54f5dbc473e502e2242c4abbcb15d79d54f5dbc473e502e224264",
        "errors": [
            {
                "errorCode": "1004",
                "errorMessage": "XXXX Not Found"
            }
        ]
    }
}

这些是我失败的测试:

tests['response json contains responseHeader'] = _.has(responseJSON, 'responseHeader');
tests['response json contains errors'] = _.has(responseJSON, 'responseHeader.publisherId');
tests["Response has publisher id"] = responseJSON.publisherId === 10003;

推荐答案

在测试"选项卡中,将您的响应正文解析为一个对象,然后使用 JavaScript 执行您的测试.

In the "Test" tab, parse your response body into an object, then use JavaScript to perform your tests.

var data = JSON.parse(responseBody);
tests["publisherId is 12345"] = data.responseHeader.publisherId === "12345";

看看 Postman 网站上的测试示例:

Take a look at the test examples at the Postman site:

https://www.getpostman.com/docs/postman/scripts/test_scripts

https://www.getpostman.com/docs/postman/scripts/test_examples