GET Request params with Axios

Published on Aug 11, 2021

1 min read

JAVASCRIPT

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:

1yarn add axios

Simple GET HTTP request with axios

🔗

A simple GET HTTP request may look like:

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

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

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

Adding parameters to GET requests

🔗

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

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

Or can use params property in the options:

1axios.get(`${BASE_URL}/movie/popular`, {
2 params: {
3 api_key: API_KEY,
4 page: pageNumber
5 }
6});

Share this article on Twitter


More Posts

Browse all posts

Join 1300+ devs & subscribe to my newsletter!

NEWSLETTER

🔗 Subscribe on Revue

AMAN MITTAL author

Software Developer and Technical Writer since 2017. Loves learning and writing about Node.js, React, React Native & Expo. Previously, a Developer Advocate at Draftbit.


Copyright ©  2019-2022 Aman Mittal · All Rights Reserved.