axios

npm version CDNJS Build status Gitpod Ready-to-Code code coverage install size npm downloads gitter chat code helpers

Promise based HTTP client for the browser and node.js

New axios docs website: click here

Table of Contents

Features

  • Make XMLHttpRequests from the browser

  • Make http requests from node.js

  • Supports the Promise API

  • Intercept request and response

  • Transform request and response data

  • Cancel requests

  • Automatic transforms for JSON data

  • Client side support for protecting against XSRF

Browser Support

Chrome

Firefox

Safari

Opera

Edge

IE

Latest ✔

Latest ✔

Latest ✔

Latest ✔

Latest ✔

11 ✔

Browser Matrix

Installing

Using npm:

Using bower:

Using yarn:

Using jsDelivr CDN:

Using unpkg CDN:

Example

note: CommonJS usage

In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with require() use the following approach:

Performing a GET request

NOTE: async/await is part of ECMAScript 2017 and is not supported in Internet Explorer and older browsers, so use with caution.

Performing a POST request

Performing multiple concurrent requests

axios API

Requests can be made by passing the relevant config to axios.

axios(config)

axios(url[, config])

Request method aliases

For convenience aliases have been provided for all supported request methods.

axios.request(config)

axios.get(url[, config])

axios.delete(url[, config])

axios.head(url[, config])

axios.options(url[, config])

axios.post(url[, data[, config]])

axios.put(url[, data[, config]])

axios.patch(url[, data[, config]])

NOTE

When using the alias methods url, method, and data properties don't need to be specified in config.

Concurrency (Deprecated)

Please use Promise.all to replace the below functions.

Helper functions for dealing with concurrent requests.

axios.all(iterable) axios.spread(callback)

Creating an instance

You can create a new instance of axios with a custom config.

axios.create([config])

Instance methods

The available instance methods are listed below. The specified config will be merged with the instance config.

axios#request(config)

axios#get(url[, config])

axios#delete(url[, config])

axios#head(url[, config])

axios#options(url[, config])

axios#post(url[, data[, config]])

axios#put(url[, data[, config]])

axios#patch(url[, data[, config]])

axios#getUri([config])

Request Config

These are the available config options for making requests. Only the url is required. Requests will default to GET if method is not specified.

Response Schema

The response for a request contains the following information.

When using then, you will receive the response as follows:

When using catch, or passing a rejection callback as second parameter of then, the response will be available through the error object as explained in the Handling Errors section.

Config Defaults

You can specify config defaults that will be applied to every request.

Global axios defaults

Custom instance defaults

Config order of precedence

Config will be merged with an order of precedence. The order is library defaults found in lib/defaults.js, then defaults property of the instance, and finally config argument for the request. The latter will take precedence over the former. Here's an example.

Interceptors

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

If you need to remove an interceptor later you can.

You can add interceptors to a custom instance of axios.

When you add request interceptors, they are presumed to be asynchronous by default. This can cause a delay in the execution of your axios request when the main thread is blocked (a promise is created under the hood for the interceptor and your request gets put on the bottom of the call stack). If your request interceptors are synchronous you can add a flag to the options object that will tell axios to run the code synchronously and avoid any delays in request execution.

If you want to execute a particular interceptor based on a runtime check, you can add a runWhen function to the options object. The interceptor will not be executed if and only if the return of runWhen is false. The function will be called with the config object (don't forget that you can bind your own arguments to it as well.) This can be handy when you have an asynchronous request interceptor that only needs to run at certain times.

Handling Errors

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

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

Cancellation

You can cancel a request using a cancel token.

The axios cancel token API is based on the withdrawn cancelable promises proposal.

You can create a cancel token using the CancelToken.source factory as shown below:

You can also create a cancel token by passing an executor function to the CancelToken constructor:

Note: you can cancel several requests with the same cancel token. If a cancellation token is already cancelled at the moment of starting an Axios request, then the request is cancelled immediately, without any attempts to make real request.

Using application/x-www-form-urlencoded format

By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.

Browser

In a browser, you can use the URLSearchParams API as follows:

Note that URLSearchParams is not supported by all browsers (see caniuse.com), but there is a polyfill available (make sure to polyfill the global environment).

Alternatively, you can encode data using the qs library:

Or in another way (ES6),

Node.js

Query string

In node.js, you can use the querystring module as follows:

or 'URLSearchParams' from 'url module' as follows:

You can also use the qs library.

NOTE

The qs library is preferable if you need to stringify nested objects, as the querystring method has known issues with that use case (https://github.com/nodejs/node-v0.x-archive/issues/1665).

Form data

In node.js, you can use the form-data library as follows:

Alternatively, use an interceptor:

Semver

Until axios reaches a 1.0 release, breaking changes will be released with a new minor version. For example 0.5.1, and 0.5.4 will have the same API, but 0.6.0 will have breaking changes.

Promises

axios depends on a native ES6 Promise implementation to be supported. If your environment doesn't support ES6 Promises, you can polyfill.

TypeScript

axios includes TypeScript definitions and a type guard for axios errors.

Online one-click setup

You can use Gitpod an online IDE(which is free for Open Source) for contributing or running the examples online.

Open in Gitpod

Resources

Credits

axios is heavily inspired by the $http service provided in Angular. Ultimately axios is an effort to provide a standalone $http-like service for use outside of Angular.

License

MIT

Last updated