Postman 是一款流行的 API 开发和测试工具,以下是一些在 Debian 系统上使用 Postman 的实用技巧:
let tpl = `{{headers.User-Agent}}`;
pm.visualizer.set(tpl, pm.response.json());
let tpl = `<table>{{#each results}}<tr><td>{{title}}</td> <td>{{release_date}}</td></tr>{{/each}}</table>`;
pm.visualizer.set(tpl, pm.response.json());
let tpl = `<!DOCTYPE html><html><head><script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.8.0/p5.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.8.0/addons/p5.sound.min.js"></script></head><body><main></main><script>function setup() {createCanvas(800, 300);frameRate(3)}function draw() {background(0);circle(random(width), random(height), 15)}</script></body></html>`;
pm.visualizer.set(tpl, pm.response.json());
GET {{base_url}}/users
{ {currentDate}}
来插入。GET {{base_url}}/data?time={ {currentDate}}
// Pre-request Script
let counter = pm.environment.get("counter") || 0;
counter++;
pm.environment.set("counter", counter);
// Test
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
pm.test("Response time is less than 500ms", function () {
pm.expect(pm.response.responseTime).to.be.below(500);
});
pm.test("Response has a users array", function () {
const response = pm.response.json();
pm.expect(response).to.have.property('users');
});
// 创建 Mock Server
pm.mockServer.create({
name: "Login API",
url: "{{api_url}}/login",
method: "POST",
body: {
mode: "formdata",
key: "username",
value: "{{username}}",
key: "password",
value: "{{password}}"
},
response: {
status: 200,
body: {
success: true,
message: "Logged in successfully"
}
}
});
通过这些实用技巧,可以更高效地使用 Postman 进行 API 开发和测试。希望这些信息对你有所帮助。