Postman - Running a request multiple times with different data sets (API testing)
HTML-код
- Опубликовано: 15 ноя 2017
- In this video, I will show you how you can take advantage of pre-request scripts and workflows in Postman to submit the same request over and over again but with different data each time.
Please note that postman.setNextRequest method takes the name of your next request as a parameter. This example only works because the request URL happens to be the same as the request name. But it does not have to be an URL, it is just a name.
If this was helpful and you want to support me create more videos like this one, please consider subscribing.
👉 / @vdespa
--
⭐️ 🎓 The complete guide to Postman (online course) ⭐️
Want to learn more about Postman? Check my Postman online course.
Get it at a special price and help support this RUclips channel:
vdespa.com/courses/
--
⭐️ 💬 I have a question. ⭐️
I do my best to answer all comments here on RUclips but I cannot make any guarantees.
If you have a question, it is best to ask your question on Discord, on the Postman User Group on Facebook, or on the Postman Community (links below).
If you are asking a question, make sure to include as many details as possible, including screenshots.
If you have purchased the Postman online course, please use the Q&A section or send me a message directly.
Please do not email me or contact me on other channels as I might not be able to answer. Sorry!
--
⭐️ 💡 I have a video idea ⭐️
Do you want me to create a video on a specific topic? Just fill out the form below:
forms.gle/uWEzXFQ2viJtZtvZ7
--
⭐️ 📺 Learn Postman on RUclips ⭐️
Collection of video tutorials:
▸ Learn Postman | bit.ly/2CFaf70
▸ Postman Crash Course | bit.ly/2YwEBBT
▸ Postman Tips & Tricks | bit.ly/2JLkXyU
___
⭐️ 📕 Free resources ⭐️
📌 DOWNLOAD the FREE Postman Quick Reference Guide
bit.ly/postman-quick-reference-yt
📌 Ask a question on Discord (unofficial)
/ discord
📌 Join the Postman User Group on Facebook (unofficial)
bit.ly/2OutAMZ
📌 Postman community forum (official)
community.getpostman.com/
--
⭐️ 📩 Contact & Imprint ⭐️
👉 Let's be friends
sendfox.com/lp/1dv56d
👉 Follow me on Twitter:
/ vdespa
👉 Follow me on Medium:
/ vdespa
👉 Follow me on Facebook:
/ valentin-despa-1130381...
👉 Let's connect on LinkedIn:
/ vdespa
vdespa.com/imprint
Download the FREE Postman Quick Reference Guide
👉 www.subscribepage.com/postman
Great video!
It helped me a lot to solve some massive integration problem with multiple customers.
Thanks!
Thanks Valentin, I found this tutorial very useful. Well explained and easy to follow.
I am glad it was helpful, Rory! Thanks for your comment.
Excellent one. Helped me in a very tight situation. Thanks.
Glad to hear that! Thank you for your comment.
Short and crisp. Thank you! :)
I am glad it matched your expectations. Thanks
More Useful and thanks for covering the scenario which I am looking for, Much Appreciated
Glad it was helpful! You are welcome.
Thank you for this helpful video. I chose to use:
if (companies instanceof Array && companies.length > 0) {
and:
if (!(companies instanceof Array)) {
instead of truthy evaluation because maybe some non-array value could evaluate to true.
Great video! Thanks for sharing.
Thank you very much, this is the exactly what tip I need right now!
Great to hear!
Thank you very much!! this is exactly I am looking....getting after a lot of searches.
Glad I could help! You are welcome. Thank you for leaving a comment.
Great Tutorial, this was super helpful!
Glad it was helpful!
Michas gracias, me ayudo mucho con este ejemplo para aplicarlo. Excelente tutorial
¡Gracias por tu comentario! Me alegro de que el tutorial haya sido útil.
Great Video!!!!!!! Thanks for the tips!!
Nice, in mobile its not visiable. Kindly post the script
Thanks Valentin. Very useful one
Thanks
Very thanks. I was trying the similar concept and got stuck you helped me. Btw how do we define pm.environment? i am using the full syntax all the time?
Thank you very much. Do you have some material for learning how to make concurrent multiple requests? Thanks in advance.
Your Postman related tutorials are great. Related to this one I do not understand the advantage of the pre-request script instead of using data file when running a collection. Seems easier for me to create a csv/json file and select it in Collection Runer.
Thank you for your comment. Regarding you remark, you are right. What I am showing is relevant when the iteration data comes from a previous request. The pre-request script was just a way to store it.
How can i achieve this if i am having multi-form data instead of raw json and file needs to be selected from the file directory.
Thanks
Thanks so much. This was such a respite for me where I had to run the APIs for 100 property ids. I could do it
Glad it helped.
Please help i want to chieve the same thing
Awesome. You saved my day, liked and subscribed your channel :)
Glad I could help! You are welcome. Thank you for leaving a comment.
Excellent Brother.
Thank you. I really appreciate it.
Thanx Valentin ;) !
@ Valentin Despa inside the if statement in the "pre-request script" you're redeclaring the variable companies. this cause alot of issue for me but it's solved by simply removing the "var" keyword inside the if statement
Glad you have found a solution. Thanks for letting me know.
Excellent tutorial. Postman could simplify the workflow though, something like just executing a JSON of request strings and then generate an output file in the end
Sure, there are many ways. Please check the router tutorial. That one shows a different approach. Thanks for the comment.
Would you recommend this technique when testing for different result codes? E.g. Checking 200, 401, 404 based on parameter values.
Thanks!
Thank you very much!
You're welcome!
Thanks for sharing
Thanks for watching!
REALLY THANKS
Most welcome
Amazing video . Thank you ! Could you please confirm if you have video for reading the data sets from CSV/Spreadsheet for data driven automation ?
Yes. There should be at least one video on this topic.
@@vdespa - Thanks . Could you please point the video
is it possible to do the same without collection runnet , just scripts in postman ? to save multiple datasets from csv file?
Hey, it is not working for multiple iterations if there is only single request in the collection
This is awesome
Thank you for your comment!
HOw can I execute same api with diiferent files in a folder. Each file is selected in the upload field as binary data ?
you Da MVP. thanks
Thanks!
Hi,
Can you suggest a solution for the below requirement
I need to form the request body from the json files present on a certain path, each json file is a separate request body.
And also the number and names of files may vary.
I am unable to pass multiple values using the approach mentioned in the video ,its only executing first value provided once only
Thanks
You are welcome!
Hello VAlentin - so this example is basically 1 iteration is set on the collection runner - but internaly the request itself loops to 3 times based on the array length ?? correct me if i understood wrong
What should i do when i have multiple variables to be changed. Example company name and address
Thanks Valentine, I found it very useful..Also can it be possible with multiple services with different testdata run at a time?
Thanks for your comment. Can you give me an example. I don't know what you mean.
@@vdespa I was able to do it now by watching one of your video with CSV file.. Thanks for your response...👍
hi @Valentin, I need to run a request URL (has dynamic values with simple auth) multiple times to get stats as 200, 400, 401, 404 on a single run. Please describe with an example
@@vdespa , ok I will try and I bought all your videos from Udemy
Put the request name inside the setNextRequest surrounded by ' ' not " " ... it won't work otherwise
Very good - but extremely quick. I am unable to see the commands in the script before you skip to the next step. It helps if you can share this collection as a JSON for us to check the commands.
Sorry for that, I will keep that in find for future videos.
@@vdespa Thanks a lot for the response :)
Do you know, if we can pass elements from excel file & verify against another excel file?
I don' thinks would be possible.
does this works in the monitor (sandbox)? I need to set up a loop just like this in a monitor, I read that postman.* operations do not work on the sandbox.
@@vdespa Hi, I asked directly to the forum and I misunderstood something, what is not available in the sandbox are some dynamic variables apparently but postman should work. Thanks for your concern!
Is there a way to do this exact same but using the console runner newman?
Yes, you can also use newman for this.
I've recently started using Insomnia Core rather than Postman but it's missing some key features. Is there a feature similar to the Postman Collection runner feature i can set up in Insomnia?
Sorry, I don't use Insomnia.
Works only if Collection Runner is used. Outside Collection Runner screen, test will run only once
This is expected as the Postman workflows can only work if you automate (Collection Runner, Newman).
Nice example but it gets a little messy if you have a very elaborate JSON body which you iteratively adapt according to a array (or multiple). Is it possible to use an external file for this as well? Let’s say CSV or XML?
This example assumes this data comes from another endpoint (I should have said that). You can use CSV or JSON. See other tutorials on this channel.
@Adam Schomaker As far my understanding it can only be achieved using Collection runner which might not make sense in a lot of cases tbh
I need to paginate to get all the data from an API, for example:
500 names per page and start from 501 next request until we reach 170K, so 340 requests. How do I increase the offset parameter by 500 each time?
Have you tried using a variable to store the last entry number?
Great video and information. I stuck in one place of API automation : I am getting an otp verification URL in one of my API response and I am opening the URL manually in one browser to enter the OTP, how to automate it because without automating this step my next request is failing. Please help me.
If you need manual intervention, you can't automate this with Postman. You may need to use another e2e tool like Cypress (or something) to do that UI interaction. Join the Discord community (see link in the description) and ask your question there. Consider adding more details, including screenshots. Thanks for understanding.
Hi Valentin, thanks for the tutorial. It was awesome. I guess what I would need some pointers on is how to write tests for each new NAME you are passing into theAPI requests. Something like: if the name is "Google", assert the response is 200, if the name is "Facebook", assert the response is 409. How can I loop through tests per request value?
Thank you for your comment. You just need to replace 200 with a variable. This will help make your tests dynamic.
@@vdespa Thanks for that pointer. I may as well declare an array of responses that match my array of inputs. This way, I can use the same SHIFT method to assert the expected result...
Thanks for the guide. I followed it from the start to finish, however when I try to run the collection, my request is executed only once and I see the "No tests found" message. Any ideas what is wrong?
"No tests found" appears when there are no tests.
I also faced the same issue: it shows no tests
Hey , I need to convert the string which I am passing in base 64 ,so how can i send multiple data with base 64.
Base64 is just the encoding. The principle stays the same. You may also want to look into creating a CSV or JSON file with the iteration data.
I have followed your example exactly as you've coded, but my post only runs once and stops after Google.
Use console.log to debug your script.
I have tried using this approach but did not work for me as expected. While running through collection runner, request runs only once and to run with next set of data i need to run collection again. Is it something i am missing that you could help? Thanks in advance.
It is hard to know what the problem is in this case. Join the Facebook community (see link in the description) and ask your question there. Consider adding more details, including screenshots. Thanks for understanding.
#need help
Instead of uploading files during Run, can we provide a URL so every time I don't need to upload files?
If you are testing a file upload, you need to actually upload a file otherwise your test is useless.
How can I pass a whole object as variable instead of just passing it as a value in Key-Value pair?
What I mean is instead of passing ----- "name": {{companies}} ------- . can I pass only ----- {{company_object}} ------, where company_object = {"name": "Company Name"}..
I have thousands of JSON objects that I need to post to my Google Cloud Storage. When I run my collection following this guide, it takes a very long time to get every JSON object inside since each request is a single JSON object that takes 200 ms. Is there a way to speed this process up? Would it be something like batching the HTTP requests?
Hi Cannot
Did you get the solution ? I also have a similar kind of issue.
I got error ' the request does not have any test '
Thanks for this video. It helps alot!!! I have a doubt, Now I have 2 variables (list) instead of 1 (list) (in your case companies) . I want to send the variable to body section one by one . I have tried but not able to do. Could you please help me.. Thanks in advance!!!
@@vdespa now in my body section there are 2 variables instead of 1 like in your case it is company and in my case time period and role. how i can send these 2 variable in nested loop like for 1 time period all roles will go and then for 2nd time period all the roles will go again. Thanks in advance!!
@@vdespa i tried it but somewhere i am going wrong.. It would be great if you could help me. Thanks in advance!!!
I am using the same code to insert [ "7666","3333","6667"] values and running through collection runner but I see only one request being called and the other are not even shown. Please help me
I changed the iteration number to multiple times in collection runner and it's accepting multiple values in arrray. Does this make sense?
@@sowmyakalvagadda6711 : this wont help, your solution to set the iteration doesn't solve the purpose.
yes same-thing for me. it's run only once
Changing iteration number helped me
@valetin Despa, I have to set the response data values for different requests in same environment variable. Is it possible? (Ex environment variable: values : 1,2,3,4). Do we have any method instead of pm.environment.set(). Set method is updating with new value. I need to add multiple values to same variable.
If you need to add multiple values to same variable, you may want to use an array to store this.
I am new to Postman and running into a recurrent issue that I can’t figure out. I am trying to run the same request multiple times using an array of data established on the Pre-request script, however, when I go to the runner the request is only running once, rather than 3 times.
Can somebody help
It is hard to know what the problem is in this case. Join the Facebook community (see link in the description) and ask your question there. Consider adding more details, including screenshots. Thanks for understanding.
in the Data Set Result, why the PASSED and FAILED is ZERO??
Good question. This is because I haven't written any tests.
Hello Valentin, how are you?
I was trying to do this in one of my tests but it doesn't work. Did something change and it's not possible to do it anymore?
THanks
Please check again your code. This should still work.
@@vdespa Yes, i just verified and working.
I did a mistake too. That was postman.sendNextRequest("reqName") instead of postman.setNextRequest("reqName");
And what if we need to execute each company request at a time instead of requesting one by one?
Actually i am working on a forex auto trading project and need to send buy requests to the websocket api server for multiple clients at a specific live market price at a time.
And if it executes one after one as you described, means each client's buy requests will be at different market prices.
Do u have any solution how to achieve? I am moving around batching MAP
@@vdespa Yes i know that...
Asked u bcz i thought u might have any solution regarding my query. Currently i am using jQuery for my websockets but can also move to c# if that fix my issue.
Waiting for ur valuable response...
@@vdespa oh okay
Hi, very useful but you missed one little part that makes it impossible to retry and repopulate the variables.
You need to check in the if in the pre-request script : if(!companies || companies.length==0)
Without the last part, the next run will have "[]" as companies and the if statement will be false as an empty array is set and is not null.
Yes, this is a valid point. The script could be improved, the concept is largely the same.
Even if you set in the pre-request script : if(!companies || companies.length==0), array = [] still and the error still occurs
Hello Valentine, I tried almost the same thing i.e passing an array of values to an api and when i executed the same API i got a message "There are no test for this request". Why am i getting this message?
As the error says, you probably did not write any tests for the request.
@@vdespa I have written the same code as that of yours.I was passing a wrong variable name and hence was getting this message. Strange though.It works now. Thanks for your response.
@@vdespa For me also it showed the same message but the multiple post operation also worked as expected.
postman.setNextRequest("xyz") throwing BAD Request 400, please suggest
Skip to 2:45
Fiz a mesma coisa e não funcionou nada..
Getting error Cannot read property 'get' of undefined
Which Postman version are you using?
How do I login to a page with different usernames and passwords
Maybe Postman is not the best tool for the job.
@@vdespa If I am to try the above on Postman, is there a way ?
This can be done much simpler.
Just add csv file as Data in Postman Collection Runner. The csv should contain only one column - "company" with all the values
And there you go. No pre or test scripts are needed.
Sure. This example is useful if you don't know the data in advance. For example if it comes from the response of a previous request.
My api is running only 1 time
Great video, Thanks!
I've my tests and pre-requisite scripts, after i go to Runner, select the collection and I let the Iterations=1 and the test just run only one time. If i change the iterations to 3 then the first value from the array is only getting executed 3 times, none of the other values are getting picked up. Please advise.
*My Prerequisite script : *
var states = pm.environment.get("states");
if (!states){
var states = ["TX", "GU", "AK"];
}
var currentState = states.shift();
pm.environment.set("stateUS", currentState);
pm.environment.set("states", states);
*My Test : *
var states = pm.environment.get("states");
if (states && states.length > 0) {
postman.setNextRequest("My request url");
console.log("Executed");
}
else
{
postman.setNextRequest(null);
}
Note: the values from "states" variable should be sequentially entered in one of the headers and response will be validated in the Tests.
I have the same problem. 1 iteration--> only one test but my array have 3 elements
same with me I have 3 elements in array , but while executed only one is executed
@@archanashinde8029 Try this: find the name of your request (it is highlighted in Collections list (after POST) then in Tests under "if" put postman.setNextRequest("NAME_OF_YOUR_REQUEST");
@@patrykjanka I have the same problem. I put the name of the request but it didn't work
@@patrykjanka This worked for me! Thank you!
Hello, I have done the steps in this example but it only seems to be sending one company at a time. It doesn't loop until it becomes invalid. Am i doing something wrong?
**PRE-REQUEST SCRIPT**
var companies = pm.environment.get("companies");
if (!companies) {
companies = ["Goooooooooogle", "Yahoo!", "Facebook", "Amazon", "Currys", "Orion", "Zienu"];
}
var currentCompany = companies.shift();
pm.environment.set("company", currentCompany);
pm.environment.set("companies", companies);
**TESTS**
var companies = pm.environment.get("companies");
if (companies && companies.length >0){
pm.sendRequest("postb.in/5HXHKNhQ", function (err, response) {
console.log(response.json);
});
}
else {
pm.sendRequest(null);
}
@@vdespa the postman.setNextRequest didn't work for me
I'm getting error - Cannot read property 'get' of undefined. Please help
Same issue with me, Only one company from array is getting picked and run, it doesn't run for other.
@@Danish-797 I have same error, Does somebody know why?
same foe me it works for only one element from array. Please reply with solution
Pretty unnatural for a tool used by hundreds of companies and thousands of developers. And pretty industrious. What if you have 100 urls to automate, not 100 but 50, what if you have 100 values for one url, you will have to create an array with 100 values, pretty uncomfortable too. This is one of those features that could be so naturally embedded on the app, but for some reason is not, like Microsoft they just decide that something that should be so easy must be difficult and time consuming.
Hi, Valentin! Can you help me pls, what if in "companies" instead of array ["name1", "name2"] i would like to get Environment Variable with array of numbers (in my case) that looks like [1,2,3] and i want my requests to loop through this array, if i simply put in "companies" : pm.environment.get("my variable"); all i get is all array in my request body parametr
Sorry for the late reply. Are you still facing this problem? Maybe you can join Discord and ask there.