Search code examples
reactjsapi

How can I make SpaceX API query params work?


I'm practicing with the SpaceX API and everything works instead of the query API call. It actually works but the params that I sent and response that I get isn't accurate, so it always returns 10 results no matter what limit I write.

const API = "https://api.spacexdata.com/v4";
const params = {
  offset: 30,
  limit: 10,
};
return axios
  .post(`${API}/launches/query`, { params })
  .then((res) => {
    console.log(res.data);
  })
  .catch((e) => console.error("Error: ", e));

So I do get the response but the number of launches I get is always 10 no matter what LIMIT I set. Can someone point me to what I'm doing wrong and what is my code missing?


Solution

  • According to the SpaceX API, in order to filter requests, you have to add the following JSON structure to the request:

    {
      "query": {},
      "options": {}
    }
    

    Currently you're missing the "options" from your request, and the API ignores your filter. As per that, you should modify your post call parameter to the following:

    axios.post(`${API}/launches/query`, { options: params })...