Hi all - I’m trying to skip a request in a collection when the cell in my CSV is blank but I can’t figure out how to reference this in the pre-request script.
In this example field1, field2 and field3 are individual PUT requests. On the 1st iteration it should skip field3, on the 2nd if should skip field1, on the 3rd it should skip field2 etc.
I think your issue may have been answered in another post a little while ago. There are a couple recommendations for solutions in there. The comment that I added at the end may be applicable to your issue as well. Let me know if there are still concerns.
I’m trying to skip the request if the pre-request script detects an empty value in the csv. I’ve checked your post but I’m still unsure on how to apply this. I’ve tried the below to detect an empty cell value and then setNextRequest to skip to the next request but I can’t seem to get it working. Do you have any other ideas?
So, that code should work but you need to put it into a separate Request.
So, for your case, you have your PUT Request right now that you are trying to use the Pre Request to skip the Request if there is no value. This is not possible as all the Pre Request script is really for is setting up the data to use for the Request. postman.SetNextRequest will do nothing in the Pre Prequest.
This is where a Setup Request can help you. So, for your case, create a new Request (I usually call it something like “Setup”) and put it above your PUT Request in the Collection. Set it up as a GET and point to any endpoint that you have (I usually use a call to a ping API Endpoint if I have one). Dont put anything in the Pre Request or Body of the GET Request, as the details are not important. In the GET Tests, this is where you put that script that you added. This allows you to control whether or not the PUT Request is called or skips to the next one in the Iteration.
Oh, and something to add, you will want to direct the postman.setNextRequest for if there is a value or not, to ensure that you control for when there is a value and when there is not.
For Example:
if(pm.iterationData.get(field1) == “”) {
postman.setNextRequest(“request_field2”);
} else {
postman.setNextRequest(null); //This ends the current iteration and moves onto the next
}
//NOTE: you can change the null to any Request you want, I just wanted to show how to end the iteration if needed.