How to read Response Header with pagination metadata. #476
-
|
Hi, I am trying to read the response header to implement the pagination but I am not able to do it. Thanks in Advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
ky returns standard const response = await ky.get('https://api.example.com/items');
// read the X-Pagination header
const paginationHeader = response.headers.get('X-Pagination');
const pagination = JSON.parse(paginationHeader);
console.log(pagination.totalRecords);
console.log(pagination.pageNumber);
console.log(pagination.totalPages);
// get the body
const data = await response.json();or combine it in one call: async function fetchWithPagination(url) {
const response = await ky.get(url);
return {
data: await response.json(),
pagination: JSON.parse(response.headers.get('X-Pagination'))
};
}
// usage
const {data, pagination} = await fetchWithPagination('https://api.example.com/items');note: if you use |
Beta Was this translation helpful? Give feedback.
ky returns standard
Responseobjects, so you can access headers directly:or combine it in one call: