Axios is a popular JavaScript library used for making HTTP requests from the browser or Node.js. It provides an easy-to-use API for sending and receiving data over the internet.
Axios is built on top of the XMLHttpRequest (XHR) API, which is a browser API that allows web applications to send and receive data over the HTTP protocol. Axios provides a simple and intuitive interface for making HTTP requests, handling responses, and managing errors.
Axios is known for its ability to handle different types of data, including JSON, XML, and binary data. It also supports various HTTP methods, including GET, POST, PUT, DELETE, and PATCH.
Axios also provides a number of advanced features, such as interceptors, which allow you to modify requests and responses before they are sent or received. It also supports cancellation, which allows you to cancel a request before it is completed.
This cheat sheet provides an overview of the most commonly used features of Axios.
Installation
Axios can be installed using npm or yarn:
npm install axios
yarn add axios
Importing Axios
Axios can be imported into your project using the following syntax:
import axios from 'axios';
Making Requests
Axios provides several methods for making HTTP requests. The most commonly used methods are:
Method | Description |
---|---|
axios.get(url[, config]) | Sends a GET request to the specified URL. |
axios.post(url[, data[, config]]) | Sends a POST request to the specified URL with the specified data. |
axios.put(url[, data[, config]]) | Sends a PUT request to the specified URL with the specified data. |
axios.patch(url[, data[, config]]) | Sends a PATCH request to the specified URL with the specified data. |
axios.delete(url[, config]) | Sends a DELETE request to the specified URL. |
All of these methods return a Promise that resolves with the response data.
Request Configurations
Axios allows you to configure your requests using an optional config
object. The most commonly used configuration options are:
Option | Description |
---|---|
params | An object containing query parameters to be appended to the URL. |
data | The data to be sent with the request. |
headers | An object containing custom headers to be sent with the request. |
timeout | The number of milliseconds before the request times out. |
withCredentials | A boolean indicating whether or not to send cookies with the request. |
responseType | The type of data expected in the response. |
Handling Responses
Axios provides several methods for handling responses. The most commonly used methods are:
Method | Description |
---|---|
response.data | The response data. |
response.status | The HTTP status code. |
response.statusText | The HTTP status message. |
response.headers | An object containing the response headers. |
response.config | The request configuration. |
Interceptors
Axios allows you to intercept requests and responses using interceptors. Interceptors are functions that are called before a request is sent or after a response is received. The most commonly used interceptors are:
Interceptor | Description |
---|---|
axios.interceptors.request.use(config => {...}) | Called before a request is sent. |
axios.interceptors.response.use(response => {...}) | Called after a response is received. |
Error Handling
Axios provides several methods for handling errors. The most commonly used methods are:
Method | Description |
---|---|
catch(error => {...}) | Called when an error occurs. |
axios.isCancel(error) | Returns true if the error is a cancellation error. |
Cancellation
Axios allows you to cancel requests using cancellation tokens. Cancellation tokens are objects that can be used to cancel a request. The most commonly used methods are:
Method | Description |
---|---|
axios.CancelToken.source() | Creates a new cancellation token. |
source.cancel() | Cancels the request associated with the cancellation token. |