README.md 5.6 KB
Newer Older
M
Matt Zabriskie 已提交
1
# axios [![Build Status](https://travis-ci.org/mzabriskie/axios.svg?branch=master)](https://travis-ci.org/mzabriskie/axios)
M
Matt Zabriskie 已提交
2

M
mzabriskie 已提交
3
Promise based HTTP client for the browser and node.js
M
Matt Zabriskie 已提交
4 5 6

## Features

M
mzabriskie 已提交
7 8
- Make [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from the browser
- Make [http](http://nodejs.org/api/http.html) requests from node.js
M
mzabriskie 已提交
9
- Supports the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) API
M
mzabriskie 已提交
10
- Transform request and response data
M
Matt Zabriskie 已提交
11
- Automatic transforms for JSON data
M
mzabriskie 已提交
12 13
- Client side support for protecting against [XSRF](http://en.wikipedia.org/wiki/Cross-site_request_forgery)
- Specify HTTP request headers
M
Matt Zabriskie 已提交
14

M
Matt Zabriskie 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
## Installing

Using bower:

```bash
$ bower install axios
```

Using npm:

```bash
$ npm install axios
```

## Compatability

Tested to work with >=IE8, Chrome, Firefox, Safari, and Opera.

M
Matt Zabriskie 已提交
33 34 35 36 37 38 39
## Example

Performing a `GET` request

```js
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
M
Matt Zabriskie 已提交
40 41 42
	.then(function (response) {
		console.log(response);
	})
M
Matt Zabriskie 已提交
43 44 45
	.catch(function (response) {
		console.log(response);
	});
M
Matt Zabriskie 已提交
46 47 48
	
// Optionally the request above could also be done as
axios.get('/user', {
M
Matt Zabriskie 已提交
49 50 51 52 53 54 55
		params: {
			ID: 12345
		}
	})
	.then(function (response) {
		console.log(response);
	})
M
Matt Zabriskie 已提交
56 57 58
	.catch(function (response) {
		console.log(response);
	});
M
Matt Zabriskie 已提交
59 60 61 62 63 64
```

Performing a `POST` request

```js
axios.post('/user', {
M
Matt Zabriskie 已提交
65 66 67 68 69 70
		firstName: 'Fred',
		lastName: 'Flintstone'
	})
	.then(function (response) {
		console.log(response);
	})
M
Matt Zabriskie 已提交
71 72 73
	.catch(function (response) {
		console.log(response);
	});
M
Matt Zabriskie 已提交
74 75 76 77 78 79
```

Aliases are provided for success and error

```js
axios.get('/user/12345')
M
Matt Zabriskie 已提交
80 81 82 83 84 85
	.success(function () {
		console.log('user found');
	})
	.error(function () {
		console.log('error finding user');
	});
M
Matt Zabriskie 已提交
86 87
```

M
Matt Zabriskie 已提交
88 89
Performing multiple concurrent requests

M
Matt Zabriskie 已提交
90
```js
M
Matt Zabriskie 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
function getUserAccount() {
	return axios.get('/user/12345');
}

function getUserPermissions() {
	return axios.get('/user/permissions/12345');
}

axios.all([getUserAccount(), getUserPermissions()])
	.then(axios.spread(function (acct, perms) {
		// Both requests are now complete
	}));
```

M
Matt Zabriskie 已提交
105 106
## Request API

M
Matt Zabriskie 已提交
107
Requests can be made by passing the relevant config to `axios`.
M
Matt Zabriskie 已提交
108

M
Matt Zabriskie 已提交
109
##### axios(config)
M
Matt Zabriskie 已提交
110 111 112

```js
axios({
M
Matt Zabriskie 已提交
113 114
	method: 'get',
	url: '/user/12345'
M
Matt Zabriskie 已提交
115 116 117 118 119 120 121
});
```

### Request method aliases

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

M
Matt Zabriskie 已提交
122 123 124 125 126 127
##### axios.get(url[, config])
##### axios.delete(url[, config])
##### axios.head(url[, config])
##### axios.post(url[, data[, config]])
##### axios.put(url[, data[, config]])
##### axios.patch(url[, data[, config]])
M
Matt Zabriskie 已提交
128 129

###### NOTE
M
Matt Zabriskie 已提交
130
When using the alias methods `url`, `method`, and `data` properties don't need to be specified in config.
M
Matt Zabriskie 已提交
131

M
Matt Zabriskie 已提交
132 133
### Concurrency

M
Matt Zabriskie 已提交
134 135
Helper functions for dealing with concurrent requests.

M
Matt Zabriskie 已提交
136 137 138
##### axios.all(iterable)
##### axios.spread(callback)

M
Matt Zabriskie 已提交
139
### Config
M
Matt Zabriskie 已提交
140

M
Matt Zabriskie 已提交
141
This is the available config options for making requests. Only the `url` is required. Requests will default to `GET` if `method` is not specified.
M
Matt Zabriskie 已提交
142

M
Matt Zabriskie 已提交
143 144 145 146 147 148
```js
{
	// `url` is the server URL that will be used for the request
	url: '/user',
	
	// `method` is the request method to be used when making the request
M
Matt Zabriskie 已提交
149
	method: 'get', // default
M
Matt Zabriskie 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
	
	// `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'
	transformRequest: [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 the success/error handlers
	transformResponse: [function (data) {
		// Do whatever you want to transform the data
		
		return data;
	}],
	
	// `headers` are custom headers to be sent
	headers: {'X-Requested-With': 'XMLHttpRequest'},
	
	// `param` are the URL parameters to be sent with the request
	params: {
		ID: 12345
	},
	
	// `data` is the data to be sent as the request body
	// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
	data: {
		firstName: 'Fred'
	},
	
M
Matt Zabriskie 已提交
181 182
	// `withCredentials` indicates whether or not cross-site Access-Control requests
	// should be made using credentials
M
Matt Zabriskie 已提交
183
	withCredentials: false, // default
M
Matt Zabriskie 已提交
184
	
M
Matt Zabriskie 已提交
185 186
	// `responseType` indicates the type of data that the server will respond with
	// options are 'arraybuffer', 'blob', 'document', 'json', 'text'
M
Matt Zabriskie 已提交
187 188 189 190 191 192 193
	responseType: 'json', // default
	
	// `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
	xsrfCookieName: 'XSRF-TOKEN', // default
	
	// `xsrfHeaderName` is the name of the http header that carries the xsrf token value
	xsrfHeaderName: 'X-XSRF-TOKEN' // default
M
Matt Zabriskie 已提交
194 195 196 197 198 199 200
}
```

## Response API

For either `success` or `error`, the following response will be provided.

M
Matt Zabriskie 已提交
201
```js
M
Matt Zabriskie 已提交
202 203 204 205 206 207 208 209 210 211 212
axios.get('/user/12345')
	.success(function (
		// `data` is the response that was provided by the server
		data,
		
		// `status` is the HTTP status code from the server response
		status,
		
		// `headers` the headers that the server responded with
		headers,
		
M
Matt Zabriskie 已提交
213
		// `config` is the config that was provided to `axios` for the request
M
Matt Zabriskie 已提交
214 215 216 217
		config
		) {
		// Do something with result
	});
M
Matt Zabriskie 已提交
218 219 220
}
```

M
Matt Zabriskie 已提交
221
## Credits
M
Matt Zabriskie 已提交
222

M
Matt Zabriskie 已提交
223 224 225
axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/ng/service/$http) provided in [Angular](https://angularjs.org/). Ultimately axios is an effort to provide a standalone `$http`-like service for use outside of Angular.

axios uses the [es6-promise](https://github.com/jakearchibald/es6-promise) polyfill by [Jake Archibald](https://github.com/jakearchibald). Until we [can use](http://caniuse.com/promises) ES6 Promises natively in all browsers, this polyfill is a life saver.
M
Matt Zabriskie 已提交
226 227 228 229

## License

MIT