Some APIs return the next page information (in pagination) as the Link header. Is there a way in Ballerina to parse the Link header and refer the different links?
An example set of Link headers are below:
Link: <https://[hostname]/config/v2/menus?pageSize=1>; rel="first"1
Link: <https://[hostname]/config/v2/menus?pageSize=1&page=5>; rel="self"2
Link: <https://[hostname]/config/v2/menus?pageSize=1&page=4>; rel="prev"3
Link: <https://[hostname]/config/v2/menus?pageSize=1&page=6>; rel="next"4
Link: <https://[hostname]/config/v2/menus?pageSize=1&page=10>; rel="last"
PS: Ballerina already supports links in the body (HATEOAS)
In Ballerina, the
Linkheader is treated similarly to any other HTTP headers since how you want to handle theLinkheader depends on your application's specific requirements. Therefore, you can use thehttp:parseHeader()function to parse theLinkheader.Here's an example:
In the above example, the resulting parsed link header is an array of header values, each of which has a
valuefield and aparamsfield. The value field contains the actual link URL, and the params field contains any other parameters specified in theLinkheader.