Is it possible to run multiple reqeusts in the Pre Request script before I run a PUT request?

I am currently trying to run 2 POST requests in my pre-request script for a PUT request, one for Authentication and the other for POSTING dummy information to be used for the PUT request test. I am currently running into a problem that is preventing me from completing the PUT request. The two post requests run in the prerequest, but the PUT request is never run… it just leaving the request hanging in Postman, and with Newman, the request is run, but returns a 404 with the variables at the end of the URL never set.

My PUT URL
https://{{SecureURL}}/api/v1/drivers/{{driver_id}}

I am wondering if it is possible to run mutliple POT requests within a pre-request?

If so, I am wondering what I am doing wrong then…

My Pre Request script:
// This API needs to have the 404 response fixed, currently returns a 400
// Set UTC timestamp to environment variable
driverNumber = Math.floor(Math.random() * 9000000000) + 1000000000;
pm.environment.set(‘timestampUtcIso8601’, (new Date()).toISOString().toLowerCase());
pm.environment.set(“user”, pm.environment.get(“timestampUtcIso8601”));
pm.environment.set(“number”, driverNumber);

pm.sendRequest({
url: ‘https://’ + pm.environment.get(“SecureURL”) + ‘/api/v1/auth/login’,
method: ‘POST’,
header: {
‘Accept’: ‘application/json’,
‘Content-Type’: ‘application/x-www-form-urlencoded’,
‘User-Agent’: ‘test’
},
body: {
mode: ‘urlencoded’,
urlencoded: [
{key: “Username”, value: pm.environment.get(“AdminUser”), disabled: false},
{key: “Password”, value: pm.environment.get(“WebPassword”), disabled: false}
]
}
}, function (err, res) {
if(err) {
console.log(“An error occurred, please review…”);
console.log(err);
} else {
pm.expect(res).to.have.property(‘code’, 200);
pm.environment.set(“token”, "Token " + res.json().token);
pm.environment.set(“subscriberId”, res.json().subscriberId);
}
});

pm.sendRequest({
url: ‘https://’ + pm.environment.get(“SecureURL”) + ‘/api/v1/drivers’,
method: ‘POST’,
header: {
‘Authorization’: pm.environment.get(“token”),
‘Accept’: ‘application/json’,
‘Content-Type’: ‘application/json’,
‘User-Agent’: ‘test’
},
body: {
mode: ‘raw’,
raw: JSON.stringify({startLocation:{address:{fullAddress:"\n, “,fullAddressInLine:”, , “,line1:“1133 Louisiana Avenue”,city:“Winter Park”,state:“FL”,zip:“32789”},latitude:28.4813018,longitude:-81.4387899},endLocation:{address:{fullAddress:”\n, “,fullAddressInLine:”, , “,line1:“1133 Louisiana Avenue”,city:“Winter Park”,state:“FL”,zip:“32789”},latitude:28.4813018,longitude:-81.4387899},user:{firstName:“Test”,lastName:“Test”,Username:”",Password:"",userId:"",role:"",isPasswordChangeRequired:false,email:“zbaker@productivityapex.com”,username: pm.environment.get(“user”),newPassword:“test”},vehicle:{},earliestStartTimeDate:“Wed Aug 01 2018 07:00:00 GMT-0400 (Eastern Daylight Time)”,restBreakDurationDate:“Wed Aug 01 2018 01:00:00 GMT-0400 (Eastern Daylight Time)”,maximumWorkingHours:9,maximumDrivingHours:8,fixedCost:0,costPerMile:0,costPerHour:0,restBreakWindows:[{startTime:“2018-07-13T16:44:30.375Z”,endTime:“2018-07-14T02:14:30.375Z”}],color:"#ff0000",tags:[{text:“test”},{text:“tag”}],skills:[{text:“test”},{text:“skill”}],driverNumber: pm.environment.get(“number”),phoneNumber:“3213131313”,earliestStartTime:“07:00:00”,restBreakDuration:“01:00:00”,customFields:{}} )
}
}, function (err, res) {
pm.expect(res).to.have.property(‘code’, 200);
pm.environment.set(“driver_id”, res.json().id);
pm.environment.set(“driverUserId”, res.json().userId);
pm.environment.set(“originalDriverObj”, JSON.stringify(res.json()));
});

Please help, as I am currently running

Hey. It’s definitely possible to send multiple requests from the pre-request script.

I use a while loop to carve up a large amount of data and POST it piece by piece to an API which imposes a maximum body length.

From your script alone I can’t say anything about why your second fails. I suggest you log your requests to console (third icon from the left at the bottom left of Postman’s window). The logged requests is what is being sent over the ‘wire’. It isn’t a template but has rendered values and can be a very quick way to spot some error that you made.

1 Like

Thank you! I appreciate the response and I apologize for the late response on my part. I found what my problem was, and it was me running an old version of postman and once I updated it to the current version, I had o problems running multiple requests int he Pre-Request script.