如何读取Postman上的Post请求内容?

问题描述 投票:1回答:1

在我测试的系统中,有的情况下,响应告知200(ok),但内容可能表示后台服务内部验证出错。如果这个服务错误代码如期而至,如何用Postman读取响应内容并安排成功验证?

testing automation postman web-api-testing
1个回答
0
投票

你可以使用Postman中的测试选项卡来运行对正文(JSON和XML)的检查。有一些片段向你展示了语法。你可以调整它们来检查响应体中指示错误的元素。


0
投票

Postman有一个名为 "测试 "的选项卡,你可以在这里提供测试脚本来执行。如果你想验证你的请求响应为200 OK,以下是脚本。

pm.test("Status test", function () {
pm.response.to.have.status(200);
});

如果你想验证响应包含任何指定的字符串。

pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("string_you_want_to_search");
});

在你的情况下,我假设可以使用上述脚本。如果响应体是JSON。

pm.test("JSON Body match", function () {
var respBody = pm.response.json();
pm.expect(respBody.<json node>).is.to.equal("Error_name");
});

JSON响应体示例 { "id" : 100, "status" : "Bad Request" }。

pm.test("JSON Body matches string", function () {
var respBody = pm.response.json();
pm.expect(respBody.status).is.to.equal("Bad Request");
});
© www.soinside.com 2019 - 2024. All rights reserved.