how to fetch all the 87 object in this api

I am trying to make a get request with axios to return all 87 objects in this api: https://swapi.py4e.com/api/people/

If we choose to show one object we modify the url like that "https://swapi.py4e.com/api/people/2"
this returns the properties of the second object in the api so I thought if I try to make a simple for loop to fetch all 87 objects one by one:

const fetchPosts = async () => {
    setLoading(true);
    let n = 1;
    for (let i = 0; i < 88; i++) {
        n = n + i;
        const res = await axios.get('https://swapi.py4e.com/api/people/' + n);
        setPosts(res.data);
    }
    setLoading(false);
}

This code has weird behaviour of returning only 12 objects and when it stops it gives error that it actually stopped at 94th and that’s not true because it actually returned only 12

the error:
enter image description here

53 thoughts on “how to fetch all the 87 object in this api”

Leave a Comment