I've got pre-requests with 2 pm.sendRequest. One of them depends on variable from first response. They work with 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 works. It takes orderId. It doesn't put in orderId variable after the first call. But I see the value in collection variables. If run it again - orderId is in variable. But it is the last orderId not new.
I use only Request Tab, not flow or anything else.
How to make pre-request get new orderId every time?
clear or unset commands doesn't help.
Local pre-request variable answers ReferenceError.
I made two requests with scrips to get/set collection variables.
It will loop call request depend on variables.
All variables handling on Pre request
or Post Request
(aka tests tab in old version)
Pre Request/Post Response
:Circle Blue
Variable | Initial value | Current value |
---|---|---|
orderIds | [1001, 1002, 1003] | [1001, 1002, 1003] |
orderId | 1001 | 1001 |
responseOrderId | null | null |
orderId
h2hRequest
parse orderId
of JSON responseProvides two APIs
h2hRequest
URL
GET /h2h/?{orderId}
Response
{
"orderId": "{Order ID}",
"name": "Name {Order ID}",
"description": "Get Order"
}
updateInfoRequest
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}`);
});
npm install express
node mock-server.js
Import this 1-demo.postman_collection.json
by Postman
{
"info": {
"_postman_id": "4c3eea37-233b-403d-b6db-b4810d80bc9a",
"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": [
"let orderIds = JSON.parse(pm.collectionVariables.get(\"orderIds\"));\r",
"\r",
"if (orderIds.length > 0){\r",
" pm.execution.setNextRequest(\"h2hRequest\"); // <- next GET Request Name\r",
"} else {\r",
" console.log(\"This is final step\")\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"
}
]
}
After Import
h2hRequest
response even it takes long processing time. we just take orderId from response JSON.h2hRequest
GET http://localhost:3000/h2h?orderId={{orderId}}
In 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);
}
In 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
with raw
JSON option
{
"orderId": {{responseOrderId}},
"name": "Name {{responseOrderId}}"
}
In updateInfoRequest, Post-response
let orderIds = JSON.parse(pm.collectionVariables.get("orderIds"));
if (orderIds.length > 0){
pm.execution.setNextRequest("h2hRequest"); // <- next GET Request Name
} else {
console.log("This is final step")
pm.execution.setNextRequest(null);
}
h2hRequest
called by orderIds
's array number items with each different orderId
If you want to re-run the collection
You should reset all collections current value
as red rectangle
orderIds : [1001, 1002, 1003]
orderId : 1001
responseOrderId : null