Hello
Iâm new to Postman. I think I have a fairly simple use case, but I cannot find a similar example which would allow me to figure this out by myself.
Situation: We use a software that offers to create lots of boards with different security configuration settings. The software does not include a report feature which would allow us to retrieve a list of the boards and their configuration. However, the software offers an API which can be used to retrieve this info. Calls to the API are limited to 50 boards per run.
The info provided is that I should use the offset parameter to adjust the set of boards returned.
So, in theory, I could run the following API requests:
https://api.company.com/v2/boards?limit=50&offset=0
https://api.company.com/v2/boards?limit=50&offset=50
https://api.company.com/v2/boards?limit=50&offset=100
https://api.company.com/v2/boards?limit=50&offset=150
âŠ
For 1600 boards, this would mean a lot of manual work. So I was hoping to be able to write a script that generates the API calls up to the point when the offset reaches 1600.
My overall idea is:
- retrieve Total (=total number of boards) and Offset from JSON response
- increment Offset by 50 each time
- use postman.setNextRequest(âGetBoardsâ) to repeat requests, until Offset value reaches Total value
I am facing two hurdles:
- Passing on the updated offset value to the 2nd request
- Prompting repeated request runs, and controlling the number of API requests carried out.
More details below. Any tips how to proceed would be much appreciated.
=== 1. Passing on the updated offset value to the 2nd request ===
TESTS tab
The following code, I hope, will increment offset after each run by 50, and place the updated value into the pm variable offsetNew
let response = pm.response.json();
let offset = response.offset
let total = response.total
console.log("Offset is: " + offset)
console.log("Total is: " + total)
offset = offset + 50
console.log("Offset + 50 is: " + offset)
pm.variables.set(âoffsetNewâ,offset)
console.log("New Offset is: " + pm.variables.get(âoffsetNewâ))
Now, Iâd need to âpushâ the value of offsetNew to be used in the next request run. I donât know how to proceed. I tried a few things:
a. Request, Pre-request script: For the first run, pm.variables.get(âoffsetNewâ) does not work â since itâs undefined.
b. Request, Body tab, ârawâ option
I tried using tab âBodyâ of the request to feed âoffsetNewâ into the request offset parameter: {âoffsetâ : {{âoffsetNewâ}}}. This doesnât work
c. Collection, Variables tab: I tried setting variable âoffsetNewâ under tab Variables in the Collection that holds my request. I canât see the impact of this.
=== 2. Prompting repeated request runs, and controlling the number of API requests carried out ===
My plan:
- Use âRun collectionâ button for the collection in which my request (âGetBoardsâ) sits
- use postman.setNextRequest(âGetBoardsâ)
- Logic: compare offsetNew against Total, and stop the requests once offsetNew reaches Total.
Since I havenât figured out how to manage increment offset, I canât test this. My initial idea is to add something like the following to tab TESTS
if (offset < total) {
postman.setNextRequest(âGetBoardsâ);
}
else {
postman.setNextRequest(null);
}