Search code examples
c#restpostmanrestsharp

Consume Rest API X-pagination from header


How can I extract the X-Pagination header from a response and use the next link to chain requests?

I've tried in both Postman and C# console application with RestSharp. No success.

Easiest would be a small console application to test. I just need to iterate through the pages.

This is what I get back in the headers X-Pagination:

{
  "Page":1,
  "PageSize":20,
  "TotalRecords":1700,
  "TotalPages":85,
  "PreviousPageLink":"",
  "NextPageLink":"www......./api/products/configurations?Filters=productid=318&IncludeApplicationPerformance=true&page=1",
  "GotoPageLinkTemplate":"www..../api/products/configurations?Filters=productid=318&IncludeApplicationPerformance=true&page=0"
}

Solution

  • In Postman you simply retrieve the header, parse it into a Json object then use the value to set a link for your next request.

    Make your initial request then in the Test tab do something like:

    var nextPageLinkJson = JSON.parse(pm.response.headers.get("X-Pagination"));
    var nextPageLink = nextPageLinkJson.NextPageLink;
    pm.environment.set("nextPageLink", nextPageLink);
    

    If you don't know how many pages you're going to have then you'll have to play with conditions when to set the nextPageLink variable and what not but that's the general idea.

    You can set the request to run using the new link with postman.setNextRequest("request_name") as well.

    Additionally this approach will only work in collection runner.