Loop request based on data from response in Postman

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there this is valentine and welcome to another postman tutorial today i wanted to show you how you can inject data from one request to the other so in this request i'm trying to submit an order and i have to provide here a tool id now the thing is i have here another endpoint which provides me with actually 20 tool ids and i need to test each of them and what i don't want to do is to go through the entire list manually or to duplicate this submit order request in this tutorial i wanted to show you a better way on how we can do this by using some scripts and automation within postman so in order to get this to work in postman what we need to do is to understand like two important concepts the first concept is about using postman variables to pass data from one request to the other and the other one is to understand how to build workflows so let's take them one by one first let's take a look at the first request and try to understand how we can get to this data we want to have essentially all the ids that we see here in the response so what we're going to do is going to write a bit of a script to extract this data from the response and put it in a separate list of ids so i'm going to switch here to the tests and the first thing that we need to do is to parse the response so i'm going to define a variable called response i'm going to parse it with pm.response.json so this will store essentially the response in a javascript object now the next thing is we don't need the entire response we are just interested in the ids that we see here so what we would like to do next is to create an array only with these ids unfortunately in javascript that's quite easy let's define another variable let's call it tool ids because this contains only ids and what we can do is we can use the map function which is available for any javascript array so we can write here something like response.map and what map does it goes through each element of the array so each element of the array is essentially each object that you see here and it would allow us to extract only the information that we're interested in so we can write something like tool because essentially every object is a tool and we're gonna write here an arrow function and i'm going to write here tool dot id okay so we can use then console.log to inspect the value of this variable and i'm going to open the console here click on send and what you will get back is a list with 20 ids so that will make our life a bit easier in terms of working with this data we only have the data that we're trying to use in the next request next what we can do is to set this in a postman variable so that we can use this data in the next request and i'm going to use a local postman variable because this is kind of like transactional data i don't want to store it in a global variable or in a collection variable essentially this data if we use local variables it will disappear after the execution but this is also the type of variables that only work in the collection runner so i'll show you in a minute what i mean by that and this pm.variables.set and we're going to set here tool ids and the value will be the same as the javascript variable so let's go now to the submit order request in this case we wanna replace this value with a postman variable and we're getting this data and we're trying to change it right before the request goes out and for that reason we'll have to work with a pre-request script because this is the kind of script that's executed before the request is being sent out if you write here a test the request has already been sent so it may be a bit too late to set any variables or to manipulate this data in somehow so going to the prequest script let's define here a variable i'm going to call it tool ids and we're going to try to extract the data that we have set in the previous request so we have here pm.variables.get and the variable name is tool ids so this is essentially the same list of variables that we had before we have set a variable and then we have this list available in the submit order now of course we can only submit one request at the time only one value at a time we cannot submit all values in the body there's only one tool id that we can submit so what you're going to try to do is to run this request multiple times but let's try and run it once so how do we get to the first id so what we're going to do here is we're going to use something like tool ids dot shift so what shift will do is we'll take the first element in the array will return it it means we can put it here in a variable we can use it and we'll also modify the original array which is the tool's id so let's say for example we want to write it in a variable called tool id so pm.variables.set we can also name it something like current tool id so that we don't have any confusion i'm gonna take this give it as a value so we're setting a variable called current tool id we're going to use this in the request so now this is the part where this becomes dynamic so we're getting the first value we're setting a variable with it we're using in the request so let's give it a try and see how this works i'm going to use here the collection runner and remember for both my local variables you cannot view it anywhere in the postman user interface if you need to view any values you have to use console.log it's going to run here this the first request has been executed the second request has been executed and if you look here at the request body we'll be able to see the tool id has been properly filled with a valid value okay so that's the first thing that we need to take care of and now the thing is well we want to use this 20 times how can we rerun this request but you know with the different data so in order to create a specific workflow in postman we will use something that's called postman set next request so postman set next request and don't confuse it with don't write pm.set next request that not work you have to write postman that set next request this function will essentially tell postman where to go next where to go after executing this request so you cannot change once you are inside the prerequest script you are still executing this request you can only decide what will happen after this and you can run set next request from the protocol script or from test doesn't really matter just going to keep it here so we have to tell postman where to go next so essentially we have extracted the first element we have set it in a variable we've used it so we want to go back essentially so where we want to go back is to this submit order so what set next request will do is we'll take a request name it's not a request url or something like that you have to name it exactly as you have here insert your collection so what we want to do here is we want to go back to the same request so we're gonna go back to submit order once again so let's take a look at what's happening right now i'm gonna run here collection you will see now that this submit order is going on and on and on and on and it's never going to stop because we haven't essentially instructed postman to run this request forever so i'm going to go here and stop run and let's take a look at what's going on so we have here the submit order we have the request body 4643 right what do we have next request body one two two five okay so obviously there's something happening here so this is changing all the time what's happening here for example we'll see here that at one point we don't have any data anymore so i'm essentially running out of data and ideally we should stop the execution so in postman the way we stop the execution is again we use postman set next request and the way we stop the execution is by saying so essentially by saying null we're telling postman after you have executed this request this current request don't go anywhere else so essentially we want to do that when we know that we have no other elements in our array essentially so the tools ids as soon as after shifting it the last time we know that it is empty it doesn't really make sense to go again and to run it again so what we'll do here we'll have to create a nif block so we'll decide here for example let's check if this is an array so we can use something like array is array just to make sure that this is really an array and then a second condition would be tool ids.length this is a property on an array tell us how many elements are in the array so as long as the length is greater than zero what we're gonna do we're gonna run this request here okay otherwise if this length is not zero then it's time to stop so let's open the collection runner once again and run it and you will see now the execution stops we can take a look at the first element what was this four six four three now we can also compare it with what we have in our list so four six four three the last element should be three four eight six so let's take a look at the last request request body three four eight six now the collection runner is not a tool that can run all the submit order in parallel so they will only work one after the other this is how this postman workflows work have to wait for one request to complete and then essentially postman looks okay where should i go next i'm going to submit order once again and so on you cannot submit all this request all together this is not the kind of workflows that postman can do now you're probably wondering how exactly did this variable with all the tool ids get updated because we have received it here on this line and then we have set the current tool id but we haven't reset the entire array and the thing is the moment we use here pm.variables that get on an object or on an array we're actually gathering a reference to the object that is stored by postman which essentially means if we're making changes to it we don't need to use pm.variables that's set again because any changes we make to the object will be automatically saved in the variable this is also the case with tool ids dot shift so when we're calling this we're making changes on the variable itself and in this case any changes we make will be automatically persisted by postman so we don't need to use pm.variables.set to set the entire rate if this was a primitive value for example a number or string we still have to use the set function but for objects and arrays this is not needed i hope this tutorial was helpful and if that was the case please consider giving this video a thumbs up leave a comment in the section below and consider subscribing for more tutorials like this one hope to see you next time bye
Info
Channel: Valentin Despa
Views: 38,351
Rating: undefined out of 5
Keywords: postman, post request using postman, postman loop, postman workflow, postman loop request, postman rerun request, postman reuse test scripts, postman reuse request
Id: 4wuvgX-egdc
Channel Id: undefined
Length: 13min 13sec (793 seconds)
Published: Thu Nov 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.