Updating the request URL after the initial call

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.

Yes, by using a collection or environment variable for the URL.

You would update that instead of using pm.request.url.

You should also have an ELSE statement that resets the variable back to the starting URL if link doesn’t exist, and then uses setNextRequest(null) to stop the loop. (To prevent you having to manually reset the starting URL each time).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.