Name: axios
Owner: Tools for GitHub
Description: Promise based HTTP client for the browser and node.js
Created: 2016-01-23 14:45:32.0
Updated: 2017-06-07 19:31:55.0
Pushed: 2016-01-23 15:25:24.0
Size: 1321
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Promise based HTTP client for the browser and node.js
| | | | | — | — | — | — | — | Latest ? | Latest ? | Latest ? | Latest ? | 8+ ? |
Using bower:
wer install axios
Using npm:
m install axios
Performing a GET
request
ake a request for a user with a given ID
s.get('/user?ID=12345')
hen(function (response) {
console.log(response);
atch(function (response) {
console.log(response);
;
ptionally the request above could also be done as
s.get('/user', {
params: {
ID: 12345
}
hen(function (response) {
console.log(response);
atch(function (response) {
console.log(response);
;
Performing a POST
request
s.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
hen(function (response) {
console.log(response);
atch(function (response) {
console.log(response);
;
Performing multiple concurrent requests
tion getUserAccount() {
turn axios.get('/user/12345');
tion getUserPermissions() {
turn axios.get('/user/12345/permissions');
s.all([getUserAccount(), getUserPermissions()])
hen(axios.spread(function (acct, perms) {
// Both requests are now complete
);
Requests can be made by passing the relevant config to axios
.
s({
thod: 'get',
l: '/user/12345'
For convenience aliases have been provided for all supported request methods.
When using the alias methods url
, method
, and data
properties don't need to be specified in config.
Helper functions for dealing with concurrent requests.
You can create a new instance of axios with a custom config.
instance = axios.create({
seURL: 'https://some-domain.com/api/',
meout: 1000,
aders: {'X-Custom-Header': 'foobar'}
The available instance methods are listed below. The specified config will be merged with the instance 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.
`url` is the server URL that will be used for the request
l: '/user',
`method` is the request method to be used when making the request
thod: 'get', // default
`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.
seURL: 'https://some-domain.com/api/',
`transformRequest` allows changes to the request data before it is sent to the server
This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
The last function in the array must return a string or an ArrayBuffer
ansformRequest: [function (data) {
// Do whatever you want to transform the data
return data;
,
`transformResponse` allows changes to the response data to be made before
it is passed to then/catch
ansformResponse: [function (data) {
// Do whatever you want to transform the data
return data;
,
`headers` are custom headers to be sent
aders: {'X-Requested-With': 'XMLHttpRequest'},
`param` are the URL parameters to be sent with the request
rams: {
ID: 12345
`paramsSerializer` is an optional function in charge of serializing `params`
(e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
ramsSerializer: function(params) {
return Qs.stringify(params, {arrayFormat: 'brackets'})
`data` is the data to be sent as the request body
Only applicable for request methods 'PUT', 'POST', and 'PATCH'
When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash
ta: {
firstName: 'Fred'
`timeout` specifies the number of milliseconds before the request times out.
If the request takes longer than `timeout`, the request will be aborted.
meout: 1000,
`withCredentials` indicates whether or not cross-site Access-Control requests
should be made using credentials
thCredentials: false, // default
`adapter` allows custom handling of requests which makes testing easier.
Call `resolve` or `reject` and supply a valid response (see [response docs](#response-api)).
apter: function (resolve, reject, config) {
/* ... */
`auth` indicates that HTTP Basic auth should be used, and supplies credentials.
This will set an `Authorization` header, overwriting any existing
`Authorization` custom headers you have set using `headers`.
th: {
username: 'janedoe',
password: 's00pers3cret'
`responseType` indicates the type of data that the server will respond with
options are 'arraybuffer', 'blob', 'document', 'json', 'text'
sponseType: 'json', // default
`xsrfCookieName` is the name of the cookie to use as a value for xsrf token
rfCookieName: 'XSRF-TOKEN', // default
`xsrfHeaderName` is the name of the http header that carries the xsrf token value
rfHeaderName: 'X-XSRF-TOKEN' // default
The response for a request contains the following information.
`data` is the response that was provided by the server
ta: {},
`status` is the HTTP status code from the server response
atus: 200,
`statusText` is the HTTP status message from the server response
atusText: 'OK',
`headers` the headers that the server responded with
aders: {},
`config` is the config that was provided to `axios` for the request
nfig: {}
When using then
or catch
, you will receive the response as follows:
s.get('/user/12345')
hen(function(response) {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
You can intercept requests or responses before they are handled by then
or catch
.
dd a request interceptor
s.interceptors.request.use(function (config) {
// Do something before request is sent
return config;
function (error) {
// Do something with request error
return Promise.reject(error);
;
dd a response interceptor
s.interceptors.response.use(function (response) {
// Do something with response data
return response;
function (error) {
// Do something with response error
return Promise.reject(error);
;
If you may need to remove an interceptor later you can.
myInterceptor = axios.interceptors.request.use(function () {/*...*/});
s.interceptors.request.eject(myInterceptor);
You can add interceptors to a custom instance of axios.
instance = axios.create();
ance.interceptors.request.use(function () {/*...*/});
s.get('/user/12345')
atch(function (response) {
if (response instanceof Error) {
// Something happened in setting up the request that triggered an Error
console.log('Error', response.message);
} else {
// The request was made, but the server responded with a status code
// that falls out of the range of 2xx
console.log(response.data);
console.log(response.status);
console.log(response.headers);
console.log(response.config);
}
;
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.
axios depends on a native ES6 Promise implementation to be supported. If your environment doesn't support ES6 Promises, you can polyfill.
axios includes a TypeScript definition.
<reference path="axios.d.ts" />
rt axios = require('axios');
s.get('/user?ID=12345');
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.
MIT