Skip to content

GET Request params with Axios

Published:

1 min read

One of the popular libraries in JavaScript land to perform HTTP requests is axios. It is promised based and allows writing code using async await syntax.

Installation

Run the command below:

yarn add axios

Simple GET HTTP request with axios

A simple GET HTTP request may look like:

axios.get({
  url: `${BASE_URL}/movie/popular?api_key=${API_KEY}&page=1`,
  method: "get",
});

This returns a promise object. Using async await syntax, the promise object can be resolved.

export const getPopularMovies = async () => {
  try {
    return await axios.get(
      `${BASE_URL}/movie/popular?api_key=${API_KEY}&page=1`
    );
  } catch (error) {
    console.error(`[API RESPONSE ERROR]: ${error}`);
  }
};

Adding parameters to GET requests

A GET response can contain parameters. With Axios you can add parameters to the URL:

axios.get(`${BASE_URL}/movie/popular?api_key=${API_KEY}&page=1`);

Or can use params property in the options:

axios.get(`${BASE_URL}/movie/popular`, {
  params: {
    api_key: API_KEY,
    page: pageNumber,
  },
});

Previous Post
How to Offer Multi-language Support in a React Native App
Next Post
How To Integrate Firebase Authentication With an Expo App

I'm a software developer and technical writer. On this blog, I share my learnings about both fields. Recently, I have begun exploring other topics, so don't be surprised if you find something new here.

Currently, working as a documentation lead at Expo.