I work with a few APIs that paginate large responses (100+). To obtain the next set of responses, the API returns a link via headers which you’re then supposed to query.
I got tired of manually finding the link in headers and then pasting it into the Postman console so I wrote a small collection to automate this: the first call hits the endpoint of interest, fetches the link from the headers, and turns the URL into a variable at the environment level.
The second call then uses this variable as its request URL. The run continues until the API stops returning a link header.
My question is: can I do this with one call instead of two? I tried writing a test in the 1st call to update the request URL and then run the call again, but it did not work.
if (pm.response.headers.has("link")) {
//The API returns 3 URLs as a string. I'm splitting them into a list.
var link_header = pm.response.headers.get("link").split(",");
//I'm setting the desired 2nd URL in the list as a variable
var next_link_original = link_header[1];
//Cleaning up the string
var next_link_clean = next_link_original.replace("<", "").replace(">; rel=\"next\"", "");
//Setting the string as the request URL
pm.request.url = next_link_clean
//Re-running the call
postman.setNextRequest("First Request")
}
When I issue this call in Runner, the URL doesn’t change in any of the subsequent calls.