Axios Cheat Sheet

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:

MethodDescription
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:

OptionDescription
paramsAn object containing query parameters to be appended to the URL.
dataThe data to be sent with the request.
headersAn object containing custom headers to be sent with the request.
timeoutThe number of milliseconds before the request times out.
withCredentialsA boolean indicating whether or not to send cookies with the request.
responseTypeThe type of data expected in the response.

Handling Responses

Axios provides several methods for handling responses. The most commonly used methods are:

MethodDescription
response.dataThe response data.
response.statusThe HTTP status code.
response.statusTextThe HTTP status message.
response.headersAn object containing the response headers.
response.configThe 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:

InterceptorDescription
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:

MethodDescription
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:

MethodDescription
axios.CancelToken.source()Creates a new cancellation token.
source.cancel()Cancels the request associated with the cancellation token.

References