# Parsing pagination links with Postman

**URL:** <https://community.postman.com/t/parsing-pagination-links-with-postman/43023>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [January 18, 2023, 11:20pm UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023 "2023-01-18T23:20:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![david.accola](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/david.accola/32/14490_2.png) [@david.accola](https://community.postman.com/u/david.accola)\
**Post date:** [January 18, 2023, 11:20pm UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023/1 "2023-01-18T23:20:25Z")

</div>

Under Tests, I’m trying to parse the header response pagination link header for the “next” link. (So that I can set it as a variable that I’ll use to set the next URL request.) But I’m stuck on parsing it. The API I’m interacting with doesn’t return it in JSON. I can get as far as returning the entire link header contents:

pm.response.headers.get(“link”)  
console.log(link)

That gets me this block of text:  
`<https://example.com/stuff?limit=5&offset=0&filter=status%3D%27active%27>;rel="prev",<https://example.com/stuff?limit=5&offset=5&filter=status%3D%27active%27>;rel="next",<https://example.com/stuff?limit=5&offset=0&filter=status%3D%27active%27>;rel="first",<https://example.com/stuff?limit=5&offset=10&filter=status%3D%27active%27>;rel="last"`

I want to parse so that I end up only with the “next” link:  
[https://example.com/stuff?limit=5&offset=5&filter=status%3D’active’](https://example.com/stuff?limit=5&offset=5&filter=status%3D%27active%27)

Thanks for your thoughts!

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 19, 2023, 10:10am UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023/2 "2023-01-19T10:10:40Z")

</div>

Can you post the actual body of the response? To see what is being returned if its not JSON. (Text maybe).

Is the pagination information anywhere in the response (or is it just contained in the header).

---

<div class="post-metadata">

**Author:** ![david.accola](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/david.accola/32/14490_2.png) [@david.accola](https://community.postman.com/u/david.accola)\
**Post date:** [January 19, 2023, 1:15pm UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023/3 "2023-01-19T13:15:22Z")

</div>

Oh, sure. To clarify, the response itself is JSON, but does not contain the pagination information. The pagination information is contained only in the header, and always in the format of the example given here.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 19, 2023, 2:42pm UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023/4 "2023-01-19T14:42:43Z")

</div>

The following approach seems to work.

Split the response into an array, using “,” as the seperator.

Then use the JavaScript find function to link the array element that includes “next”.

```auto
var link = "<https://example.com/stuff?limit=5&offset=0&filter=status%3D%27active%27>;rel=\"prev\",<https://example.com/stuff?limit=5&offset=5&filter=status%3D%27active%27>;rel=\"next\",<https://example.com/stuff?limit=5&offset=0&filter=status%3D%27active%27>;rel=\"first\",<https://example.com/stuff?limit=5&offset=10&filter=status%3D%27active%27>;rel=\"last\""

var array = link.split(',');
const search = array.find(array => array.includes("next"));
console.log(search); // "<https://example.com/stuff?limit=5&offset=5&filter=status%3D%27active%27>;rel="next""

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/5/6528217f478ec3e34881c160ab783d13bb768174.png)

---

<div class="post-metadata">

**Author:** ![david.accola](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/david.accola/32/14490_2.png) [@david.accola](https://community.postman.com/u/david.accola)\
**Post date:** [January 19, 2023, 4:44pm UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023/5 "2023-01-19T16:44:30Z")

</div>

Excellent, thank you!

To take it one step further, what’s the best way to strip the leading `<` and trailing `>;rel="next"` out for a final result of `https://example.com/stuff?limit=5&offset=5&filter=status%3D%27active%27`

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 19, 2023, 5:04pm UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023/6 "2023-01-19T17:04:07Z")

</div>

Use the JavaScript replace function (twice).

```auto
console.log(search.replace('<', '').replace('>;rel="next"',''));

```

---

<div class="post-metadata">

**Author:** ![david.accola](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/david.accola/32/14490_2.png) [@david.accola](https://community.postman.com/u/david.accola)\
**Post date:** [January 19, 2023, 6:06pm UTC](https://community.postman.com/t/parsing-pagination-links-with-postman/43023/7 "2023-01-19T18:06:07Z")

</div>

That does the trick! Thank you!
