Axios Setup

Using npm:

$ npm install axios

Using bower:

$ bower install axios

Using yarn:

$ yarn add axios

Using jsDelivr CDN:

<script src=""></script>

Using unpkg CDN:

<script src=""></script>

Axios Handling Errors

  .catch(function (error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);

Using the validateStatus config option, you can define HTTP code(s) that should throw an error.

axios.get('/user/12345', {
  validateStatus: function (status) {
    return status < 500; // Resolve only if the status code is less than 500

Using toJSON you get an object with more information about the HTTP error.

  .catch(function (error) {

Axios Handling the response

The response for a request contains the following information.

data: {}The response that was provided by the server
status: 200The HTTP status code from the server response
statusText: 'OK'The HTTP status message from the server response
headers: {}The HTTP headers that the server responded with all header names are lowercased and can be accessed using the bracket notation
config: {}The config that was provided to `axios` for the request
request: {}The request that generated this response it is the last ClientRequest instance in node.js (in redirects) and an XMLHttpRequest instance in the browser

Axios Shorthand methods

axios.request(config)Send a REQUEST request
axios.get(url, config)Send a GET request
axios.delete(url, config)Send a DELETE request
axios.head(url, config)Send a HEAD request
axios.options(url, config)Send a OPTIONS request, data, config)Send a POST request
axios.put(url, data, config)Send a PUT request
axios.patch(url, data, config)Send a PATCH request

Axios Interceptors

You can intercept requests or responses before they are handled by then or catch.

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);

If you need to remove an interceptor later you can.

const myInterceptor = axios.interceptors.request.use(function () {/*...*/});

You can add interceptors to a custom instance of axios.

const instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});

Axios Options

url: '/user'`url` is the server URL that will be used for the request
method: 'get'`method` is the request method to be used when making the request
baseURL: ''`baseURL` will be prepended to `URL` unless `URL` is absolute. It can be convenient to set `baseURL` for an instance of Axios to pass relative URLs to methods of that instance.
transformRequest`transformRequest` allows changes to the request data before it is sent to the server This is only applicable for request methods ‘PUT’, ‘POST’, ‘PATCH’ and ‘DELETE’ The last function in the array must return a string or an instance of Buffer, ArrayBuffer, FormData or Stream You may modify the headers object.
transformResponse`transformResponse` allows changes to the response data to be made before it is passed to then/catch
headers`headers` are custom headers to be sent
params`params` are the URL parameters to be sent with the request. Must be a plain object or a URLSearchParams object
paramsSerializer`paramsSerializer` is an optional function in charge of serializing `params`
data`data` is the data to be sent as the request body. Only applicable for request methods ‘PUT’, ‘POST’, and ‘PATCH’
timeout`timeout` specifies the number of milliseconds before the request times out. If the request takes longer than `timeout`, the request will be aborted.
withCredentials`withCredentials` indicates whether or not cross-site Access-Control requests should be made using credentials
adapter`adapter` allows custom handling of requests which makes testing easier.
auth`auth` indicates that HTTP Basic auth should be used, and supplies credentials.
responseType`responseType` indicates the type of data that the server will respond with options are: ‘arraybuffer’, ‘document’, ‘json’, ‘text’, ‘stream’ browser only: ‘blob’
responseEncoding`responseEncoding` indicates encoding to use for decoding responses
xsrfCookieName`xsrfCookieName` is the name of the cookie to use as a value for xsrf token
xsrfHeaderName`xsrfHeaderName` is the name of the http header that carries the xsrf token value
onUploadProgressonUploadProgress` allows handling of progress events for uploads
onDownloadProgress`onDownloadProgress` allows handling of progress events for downloads
maxContentLength`maxContentLength` defines the max size of the http response content in bytes allowed
maxBodyLength`maxBodyLength` (Node only option) defines the max size of the http request content in bytes allowed
validateStatus`validateStatus` defines whether to resolve or reject the promise for a given
maxRedirects`maxRedirects` defines the maximum number of redirects to follow in node.js. If set to 0, no redirects will be followed.
socketPath`socketPath` defines a UNIX Socket to be used in node.js.
httpAgent` and `httpsAgent` define a custom agent to be used when performing http and https requests, respectively, in node.js. This allows options to be added like `keepAlive` that are not enabled by default.
proxy`proxy` defines the hostname and port of the proxy server.
cancelToken`cancelToken` specifies a cancel token that can be used to cancel the request
decompress`decompress` indicates whether or not the response body should be decompressed automatically. If set to `true` will also remove the ‘content-encoding’ header from the responses objects of all decompressed responses