NPM axios Package


axios

axios is a promise-based HTTP client for Node.js and the browser. It makes it easy to send asynchronous HTTP requests to REST endpoints and handle the responses. This documentation provides a comprehensive guide to using the axios package, including a usage page and implementation guide.

Usage

To install axios, run the following command in your terminal:

npm install axios

Once installed, you can import axios into your code using the following statement:

import axios from 'axios';

Making a GET request

To make a GET request, simply call the get() method on the axios instance. The get() method takes the URL of the endpoint you want to request as its first argument and an optional configuration object as its second argument.

The following example shows how to make a GET request to the https://example.com/api/v1/users endpoint:

axios.get('https://example.com/api/v1/users')
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);
  });

Making a POST request

To make a POST request, call the post() method on the axios instance. The post() method takes the URL of the endpoint you want to request as its first argument, the data you want to send as its second argument, and an optional configuration object as its third argument.

The following example shows how to make a POST request to the https://example.com/api/v1/users endpoint with data in the form of a JSON object:

axios.post('https://example.com/api/v1/users', {
  name: 'John Doe',
  email: 'john.doe@example.com',
})
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);
  });

Making a PUT request

To make a PUT request, call the put() method on the axios instance. The put() method takes the URL of the endpoint you want to request as its first argument, the data you want to send as its second argument, and an optional configuration object as its third argument.

The following example shows how to make a PUT request to the https://example.com/api/v1/users/1 endpoint with data in the form of a JSON object:

axios.put('https://example.com/api/v1/users/1', {
  name: 'John Doe',
  email: 'john.doe@example.com',
})
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);
  });

Making a DELETE request

To make a DELETE request, call the delete() method on the axios instance. The delete() method takes the URL of the endpoint you want to request as its first argument and an optional configuration object as its second argument.

The following example shows how to make a DELETE request to the https://example.com/api/v1/users/1 endpoint:

axios.delete('https://example.com/api/v1/users/1')
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);
  });

Configuration options

The axios package provides a number of configuration options that allow you to customize the behavior of HTTP requests. These options can be set when you create an axios instance or by passing an optional configuration object to the request methods.

The following table lists the most common configuration options:

| Option | Description |
|—|—|
| baseURL | The base URL for all requests made by the instance. |
| timeout | The maximum amount of time (in milliseconds) to wait for a request to complete. |
| headers | An object containing headers to be sent with each request. |
| data | The data to be sent with the request. |
| params | An object containing query parameters to be sent with the request. |
| responseType | The expected response type from the server. |
| onUploadProgress | A callback that will be called periodically during file uploads to report progress. |
| onDownloadProgress | A callback that will be called periodically