jsonplaceholder icon indicating copy to clipboard operation
jsonplaceholder copied to clipboard

Is there a way to paginate the results? i.e. ?page=1 or ?limit=10

Open alex2118 opened this issue 7 years ago • 9 comments
trafficstars

Is there a way to paginate the results? i.e. ?page=1 or ?limit=10

alex2118 avatar Apr 27 '18 22:04 alex2118

Yes, a list of all the routes you can use is here: https://github.com/typicode/json-server

ghost avatar May 04 '18 20:05 ghost

Yes, this is easy . use this like

http://jsonplaceholder.typicode.com/photos?_start=0&_limit=5

singhBijendraIOS avatar May 13 '19 07:05 singhBijendraIOS

I've documented pagination in PR #105 which should make it easier to spot this feature.

dotnetCarpenter avatar Aug 14 '19 23:08 dotnetCarpenter

Here's an example of the pagination made with JSON placeholder and Nuxtjs. https://codesandbox.io/embed/pagination-browser-back-broken-6mu4r

sab-exp avatar Sep 09 '19 09:09 sab-exp

This doesn't seem to work as far as I can tell, I keep getting "Application Error" whenever I pass any query parameters.

skipjack avatar Apr 14 '20 15:04 skipjack

@skipjack please go to a site that does not have Content Security Policy (CSP) directive connect-src set to self and copy/paste the following into your console (ctrl+shift+i):

fetch('https://jsonplaceholder.typicode.com/posts?_page=1&_limit=2')
  .then(async response => {
    const link = response.headers.get('link') // link to next page (REST)
    const json = await response.json() // data payload
    console.log(link, json)
  })

What do you get back? If you get an array with 2 objects, then it works.

In the Speed Dial page in Opera (ctrl+t), I get the following output:

0: {userId: 1, id: 1, title: "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body: "quia et suscipit↵suscipit recusandae consequuntur …strum rerum est autem sunt rem eveniet architecto"}
1: {userId: 1, id: 2, title: "qui est esse", body: "est rerum tempore vitae↵sequi sint nihil reprehend…aperiam non debitis possimus qui neque nisi nulla"}
length: 2
__proto__: Array(0)

https://github.com/ has CSP connect-src, so you can not test it on this site.

dotnetCarpenter avatar Apr 25 '20 12:04 dotnetCarpenter

GET /posts?_page=7 GET /posts?_page=7&_limit=20

instruction : https://github.com/typicode/json-server/blob/master/README.md#paginate

i-zakiro avatar Aug 19 '22 09:08 i-zakiro

would be nice to include the total number of pages in the response or am I missing something?

MattBevis avatar Jan 04 '24 22:01 MattBevis

would be nice to include the total number of pages in the response or am I missing something?

I also needed the same thing, and it's there, it's inside response headers under key x-total-count.

fetch('https://jsonplaceholder.typicode.com/posts?_start=1&_limit=10')
  .then(response => {
    // Accessing the headers
    const headers = response.headers;

    const totalCount = headers.get('x-total-count') // total count of elements
    console.log(totalCount);
    return response.json();
  })
  .then(data => {
    console.log('Data:', data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

lazarevic-00 avatar Jan 13 '24 20:01 lazarevic-00