What is Axios?

Axios is a lightweight HTTP client based on the XMLHttpRequests service. It is similar to the Fetch API and is used to perform HTTP requests.

svg viewer

Installing Axios

The following command can be used for installing axios using npm:

npm install axios

Making an HTTP GET request

Axios can be used to perform a simple GET request. The syntax for this is​:

axios({
  url:'https://www.educative.io/',
  method: 'get'
})

Making an HTTP POST request

Axios can similarly be used to perform a simple POST request. We can also send parameters in an object using the POST request. The syntax for this is:

axios({
  method: 'post',
  url: '/signup',
  data: {
    user: 'abcd1234',
    pass: 'pwd1234'
  }
})

Advantage of Axios over Fetch API

  • supports older browsers
  • has a way to abort a request
  • has a way to set a response timeout
  • has built-in CSRF protection
  • supports upload progress
  • performs automatic JSON data transformation.

Copyright ©2024 Educative, Inc. All rights reserved