我收到了 2 pm.sendRequest 的预先请求。其中一个取决于第一响应的变量。 他们与 setTimeout 一起工作。
//I set Timeout because updateInfoRequest needs to wait and get it from h2hRequest response
setTimeout("updateInfo", 3000)
updateInfoBody = {"orderId":pm.collectionvariables.get("orderId")}
updateInfoRequest = {
there is connection info
body:JSON.stringify(updateInfoBody)
}
pm.sendRequest(h2hRequest, function(error, h2hResponse){
pm.collectionvariables.set(h2hResponse.json().orderId
})
function updateInfo(){
pm.sendRequest(updateInfoRequest, function(error, updateInfoResponse){
})
}
h2hRequest 有效。它需要 orderId。 第一次调用后它不会放入 orderId 变量。但我看到了集合变量的价值。 如果再次运行 - orderId 处于变量中。但这是最后一个 orderId 不是新的。
我只使用请求选项卡,不使用流程或其他任何东西。
如何让预请求每次都能得到新的orderId?
清除或取消设置命令没有帮助。
本地预请求变量回答ReferenceError。
我用脚本发出了两个请求来获取/设置集合变量。 它将根据变量循环调用请求。 所有变量在
Pre request
或 Post Request
(旧版本中的测试选项卡) 上处理
Collection Variables
:蓝色
Call Order
:红色
Pre Request/Post Response
:蓝色圆圈
Request
:绿色
Start or End
:橙色
变量 | 初始值 | 当前值 |
---|---|---|
订单ID | [1001, 1002, 1003] | [1001, 1002, 1003] |
订单ID | 1001 | 1001 |
响应订单Id | 空 | 空 |
orderId
h2hRequest
后解析JSON响应orderId
h2h请求
URL
GET /h2h/?{orderId}
Response
{
"orderId": "{Order ID}",
"name": "Name {Order ID}",
"description": "Get Order"
}
更新信息请求
URL
POST /update-info/
Input Body
{
"orderId": {Order ID},
"name": "Name {Order ID}"
}
Response
{
"orderId": {Order ID},
"name": "Name {Order ID}",
"description": "Update Order"
}
mock-server.js
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json()); // To parse JSON bodies
// GET /h2h endpoint
app.get('/h2h', (req, res) => {
const orderId = req.query.orderId;
if (orderId === undefined || orderId === null) {
return res.status(400).json({ error: 'orderId is required' });
}
console.log("/h2h" + orderId);
res.json({
orderId: orderId,
name: `Name ${orderId}`,
description: 'Get Order'
});
});
// POST /update-info endpoint
app.post('/update-info', (req, res) => {
const { orderId } = req.body;
if (orderId === undefined || orderId === null) {
return res.status(400).json({ error: 'orderId is required' });
}
console.log("/update-info" + orderId);
res.json({
orderId: orderId,
name: `Name ${orderId}`,
description: 'Update Order'
});
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
安装依赖运行Mock Servernpm install express
node mock-server.js
客户邮递员1-demo.postman_collection.json
{
"info": {
"_postman_id": "ef4bd661-b94e-4099-b512-a2268c5d5fc5",
"name": "1-demo",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1826150"
},
"item": [
{
"name": "h2hRequest",
"event": [
{
"listen": "test",
"script": {
"exec": [
"const responseJson = pm.response.json();\r",
"pm.collectionVariables.set(\"responseOrderId\", responseJson.orderId)\r",
"\r",
"console.log(\"responseOrderId: \" + pm.collectionVariables.get(\"responseOrderId\"))\r",
""
],
"type": "text/javascript",
"packages": {}
}
},
{
"listen": "prerequest",
"script": {
"exec": [
"let orderIds = JSON.parse(pm.collectionVariables.get(\"orderIds\"));\r",
"pm.collectionVariables.set(\"orderId\", orderIds.shift())\r",
"pm.collectionVariables.set(\"orderIds\", JSON.stringify(orderIds));\r",
"orderIds = JSON.parse(pm.collectionVariables.get(\"orderIds\"));\r",
"\r",
"if (pm.collectionVariables.get(\"orderId\")){\r",
" pm.execution.setNextRequest(\"updateInfoRequest\"); // <- Next Request Name\r",
"} else {\r",
" console.log(\"End\")\r",
" pm.execution.setNextRequest(null);\r",
"}"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:3000/h2h?orderId={{orderId}}",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"h2h"
],
"query": [
{
"key": "orderId",
"value": "{{orderId}}"
}
]
}
},
"response": []
},
{
"name": "updateInfoRequest",
"event": [
{
"listen": "prerequest",
"script": {
"exec": [
""
],
"type": "text/javascript",
"packages": {}
}
},
{
"listen": "test",
"script": {
"exec": [
"if (pm.collectionVariables.get(\"orderId\")){\r",
" pm.execution.setNextRequest(\"h2hRequest\"); // <- next GET Request Name\r",
"} else {\r",
" pm.execution.setNextRequest(null);\r",
"}"
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"orderId\": {{responseOrderId}},\r\n \"name\": \"Name {{responseOrderId}}\"\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:3000/update-info",
"protocol": "http",
"host": [
"localhost"
],
"port": "3000",
"path": [
"update-info"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"key": "orderIds",
"value": "[1001, 1002, 1003]",
"type": "string"
},
{
"key": "orderId",
"value": "1001",
"type": "string"
},
{
"key": "responseOrderId",
"value": "null"
}
]
}
导入后
h2hRequest
响应甚至需要很长的处理时间。我们只是从响应 JSON 中获取 orderId。
h2hRequest
GET http://localhost:3000/h2h?orderId={{orderId}}
在 h2hRequest 中,Pre-request
let orderIds = JSON.parse(pm.collectionVariables.get("orderIds"));
pm.collectionVariables.set("orderId", orderIds.shift())
pm.collectionVariables.set("orderIds", JSON.stringify(orderIds));
orderIds = JSON.parse(pm.collectionVariables.get("orderIds"));
if (pm.collectionVariables.get("orderId")){
pm.execution.setNextRequest("updateInfoRequest"); // <- Next Request Name
} else {
console.log("End")
pm.execution.setNextRequest(null);
}
在 h2hRequest 中,Post-response
const responseJson = pm.response.json();
pm.collectionVariables.set("responseOrderId", responseJson.orderId)
console.log("responseOrderId: " + pm.collectionVariables.get("responseOrderId"))
updateInfoRequest
POST http://localhost:3000/update-info
Input Body
带有
raw
JSON 选项
{
"orderId": {{responseOrderId}},
"name": "Name {{responseOrderId}}"
}
在 updateInfoRequest 中,Post-response
if (pm.collectionVariables.get("orderId")){
pm.execution.setNextRequest("h2hRequest"); // <- next GET Request Name
} else {
pm.execution.setNextRequest(null);
}
运行集合h2hRequest
由
orderIds
的数组编号项调用,每个不同的
orderId