I’m trying to run 2 POST requests to my API within the Pre-Req script. Between the first and second POSTs, I need a value to be captured and used in the next POST. The first POST will return a value in the response that I need to be used in the second POST. I cannot seem to find a way to be able to capture it from the first POST response to filter into the next POST request.
What I have in Pre-Req (Very paired down)
pm.variables.set(“RNG”, _.random(1, 999999));
pm.sendRequest({
url: ‘https://gwapi/v4/gateway/payments/’,
method: ‘POST’,
header: {
‘connectionUser’: ${pm.environment.get('ConnectionU')}
,
‘connectionPassword’: ${pm.environment.get('ConnectionP')}
,
‘accountNumber’: ${pm.environment.get('AcctNum')}
,
‘Content-Type’: ‘application/json’
},
body: {
mode: ‘raw’,
raw: JSON.stringify({
“order”: {
“orderID”: “VI” + ${pm.variables.get('RNG')}
,
“amount”: “50000”,
},
“mit”: {
“mitMsgType”: “CGEN”,
“mitStoredCredentialInd”: “N”,
“mitSubmittedTransactionID”: “”
}
})
}
},
function (err, res) {
response = res.json();
pm.expect(res).to.have.property(‘code’, 200);
pm.expect(response.order.status.hostRespCode).to.eql(“100”);
let TXID = response.order.mitReceivedTransactionID;
pm.variables.set(“TXID”, TXID);
})
pm.variables.set(“RNG”, _.random(1, 999999));
pm.sendRequest({
url: ‘https://gwapi/v4/gateway/payments/’,
method: ‘POST’,
header: {
‘connectionUser’: ${pm.environment.get('ConnectionU')}
,
‘connectionPassword’: ${pm.environment.get('ConnectionP')}
,
‘accountNumber’: ${pm.environment.get('AcctNum')}
,
‘Content-Type’: ‘application/json’
},
body: {
mode: ‘raw’,
raw: JSON.stringify({
“order”: {
“orderID”: “VI” + ${pm.variables.get('RNG')}
,
“amount”: “30200”,
},
“mit”: {
“mitMsgType”: “MRAU”,
“mitStoredCredentialInd”: “Y”,
“mitSubmittedTransactionID”: ${pm.variables.get('TXID')}
}
})
}
},
function (err, res) {
response = res.json();
pm.expect(res).to.have.property(‘code’, 200);
pm.expect(response.order.status.hostRespCode).to.eql(“100”);
})
A part of the response from the first POST looks something like this:
{
“order”: {
“orderID”: “VI647232”,
“mitReceivedTransactionID”: “012249692166917”
“status”: {
“procStatus”: “0”,
“procStatusMessage”: “Approved”,
“hostRespCode”: “100”,
“respCode”: “00”
}
}
}
The “mitReceivedTransactionID” value is what needs to be used in the second POST
I know traditionally, you can’t assign a variable inside a block then use it outside the block, however, it does assign the variable to be used in the “Body” of the transaction ran after Pre-req has ran. Everything else works, as I’ve built many others, this one just requires more than one API call in pre to be processed. And both run as intended, but i get an “undefined” where the value should be.