当Postman请求失败时,首先关注错误提示信息(如状态码、响应体中的错误详情)。例如:
404表示接口路径错误,500表示服务器内部错误,401表示未授权;{"error": "Invalid username"})会明确指出具体问题。ping www.google.com),避免因网络中断导致请求失败;Content-Type: application/json,若漏掉会导致服务器无法解析;Authorization tab中的凭证(如API Key、OAuth Token)是否有效,避免401 Unauthorized错误。libgconf-2.so.4: cannot open shared object file),需安装对应依赖:sudo apt-get update
sudo apt-get install libgconf-2-4 libcanberra-gtk-module
.tar.gz格式),或使用Snap安装(sudo snap install postman);~/.config/Postman),解决因缓存导致的配置冲突。在Postman的Tests标签页中编写JavaScript断言,自动化检查错误场景。常见断言包括:
pm.test("Status code is an error (≥400)", function () {
pm.expect(pm.response.code, `Expected status code < 400, but got ${pm.response.code}`).to.be.below(400);
});
error、message),确认错误内容符合预期:pm.test("Error message is correct", function () {
const jsonData = pm.response.json();
pm.expect(jsonData.error).to.eql("Invalid username"); // 根据实际接口返回调整字段名
});
pm.test("Response time is acceptable", function () {
pm.expect(pm.response.responseTime).to.be.below(2000); // 单位:毫秒
});
这些断言会在请求发送后自动运行,结果在Test Results面板中显示(绿色为通过,红色为失败)。
SSL certificate problem(如自签名证书),需安装系统CA证书:sudo apt-get update
sudo apt-get install ca-certificates
http://proxy.example.com:8080),避免因代理拦截导致请求失败。通过以上步骤,可系统性地处理Ubuntu中Postman的错误响应,从快速定位问题到自动化验证,提升API测试的效率和可靠性。