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);
}