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

Posted

技术标签:

【中文标题】如何在 Postman 上读取 Post 请求的内容?【英文标题】:How to read the contents of a Post request on Postman? 【发布时间】:2020-09-27 13:14:15 【问题描述】:

在我正在测试的系统中,存在响应通知 200(ok)的情况,但内容可能表明后端服务的内部验证出现错误。如果此服务错误代码按预期出现,我如何使用 Postman 读取响应内容并安排成功验证?

【问题讨论】:

【参考方案1】:

您可以使用 Postman 中的测试选项卡对正文(JSON 和 XML)运行检查。有 sn-ps 向您展示语法。您可以调整它们以检查响应正文中指示错误的元素。

【讨论】:

【参考方案2】:

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 响应正文 “身份证”:100, “状态”:“错误请求”

pm.test("JSON Body matches string", function () 
var respBody = pm.response.json();
pm.expect(respBody.status).is.to.equal("Bad Request");
);

【讨论】:

以上是关于如何在 Postman 上读取 Post 请求的内容?的主要内容,如果未能解决你的问题,请参考以下文章

postman怎么发送post请求

如何在postman的post请求中传递字符串?

postman get请求和post请求区别

postman---post请求数据类型

我无法使用 POSTMAN 创建 POST 请求,但在 Swagger 上执行此操作没有问题

Postman:如何在请求正文中使用环境变量