集合变量在 Postman 中不更新

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

我收到了 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。

postman postman-pre-request-script
1个回答
0
投票

概述

我用脚本发出了两个请求来获取/设置集合变量。 它将根据变量循环调用请求。 所有变量在

Pre request
Post Request
(旧版本中的测试选项卡)

上处理

enter image description here

颜色代码

Collection Variables
蓝色enter image description here

Call Order
红色Red

Pre Request/Post Response
蓝色圆圈enter image description here

Request
绿色enter image description here

Start or End
橙色enter image description here

集合变量

变量 初始值 当前值
订单ID [1001, 1002, 1003] [1001, 1002, 1003]
订单ID 1001 1001
响应订单Id
  • orderIds:数组数据,会赋值
    orderId
  • orderId:会调用h2hRequest输入查询
  • responseOrderId:调用
    h2hRequest
    后解析JSON响应
    orderId

enter image description here

模拟服务器

提供两个API

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 Server

npm 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" } ] }
导入后

enter image description here

    注意:无需等待 3000(3 秒),因为
  • 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); }

enter image description here

在 h2hRequest 中,

Post-response


const responseJson = pm.response.json(); pm.collectionVariables.set("responseOrderId", responseJson.orderId) console.log("responseOrderId: " + pm.collectionVariables.get("responseOrderId"))

enter image description here

updateInfoRequest


POST http://localhost:3000/update-info

Input Body

 带有 
raw
 JSON 选项

{ "orderId": {{responseOrderId}}, "name": "Name {{responseOrderId}}" }

enter image description here

在 updateInfoRequest 中,

Post-response


if (pm.collectionVariables.get("orderId")){ pm.execution.setNextRequest("h2hRequest"); // <- next GET Request Name } else { pm.execution.setNextRequest(null); }
运行集合

enter image description here

结果

h2hRequest

orderIds
 的数组编号项调用,每个不同的 
orderId

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.