Creating and using variables between two pre-req posts

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.

Can you use the preformatted text option when posting code, as its hard to read without the indents.

My first observation is that sendRequest’s are asynchronous, so you can’t guarantee the order they will run in unless you layer your code with an IF statement to first check the response before proceeding with the second sendRequest.

Have you tried saving this to a collection variable instead? So it available collection wide?
What happens if you try and console.log it outside of the original block. As you mentioned, its probably not available outside of that script block, so “undefined” would be correct.

Hi there,
Sorry, I’m not familiar with this yet and don’t know how to reformat it. I copied/pasted from Notepad++

It’ll save the value to the environment variables only after the pre-req has ran. I used a few console.log requests to confirm when it was recording the correct value. And because the API I’m testing returns a unique number in each response, I can’t just have a set value. So the console.log will return “undefined” until Pre-req is complete and shows the number once it tries sending the body. It’s a unique requirement for this series, and needs this sequence. I didn’t want to have a separate POST in the collection but it might be necessary this time. Thanks for the feedback!

I would only use pre-req to setup a test if I needed to then run a data driven test on the main request.

For example, someone raised an issue the other day, where they wanted to run a data driven test against 10 sets of data, but needed it to run against the same authorisation and initial set of data. which would change each time if you ran the tests in the normal order.

You could use the pre-req for test setup, but its just easier to put the POST\GET requests as separate requests.

I would just use the requests in order and pass the variables that way.
I don’t see any advantage to using pre-req in this circumstance but can see a few disadvantages due to the asynchronous nature.

It can be done the way you want, but you have to layer the requests in an IF statement, where the 2nd\3rd sendRequest only runs if the first one has passed some logic (at which point you should have set the collectionVariable for future use, and this includes in the next sendRequest).