jsonplaceholder
jsonplaceholder copied to clipboard
Is there a way to paginate the results? i.e. ?page=1 or ?limit=10
Is there a way to paginate the results? i.e. ?page=1 or ?limit=10
Yes, a list of all the routes you can use is here: https://github.com/typicode/json-server
Yes, this is easy . use this like
http://jsonplaceholder.typicode.com/photos?_start=0&_limit=5
I've documented pagination in PR #105 which should make it easier to spot this feature.
Here's an example of the pagination made with JSON placeholder and Nuxtjs. https://codesandbox.io/embed/pagination-browser-back-broken-6mu4r
This doesn't seem to work as far as I can tell, I keep getting "Application Error" whenever I pass any query parameters.
@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.
GET /posts?_page=7 GET /posts?_page=7&_limit=20
instruction : https://github.com/typicode/json-server/blob/master/README.md#paginate
would be nice to include the total number of pages in the response or am I missing something?
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);
});