axios 0.17.1 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of axios might be problematic. Click here for more details.
- package/CHANGELOG.md +114 -0
- package/LICENSE +1 -1
- package/README.md +102 -30
- package/dist/axios.js +615 -548
- package/dist/axios.map +1 -1
- package/dist/axios.min.js +4 -4
- package/dist/axios.min.map +1 -1
- package/index.d.ts +40 -15
- package/lib/adapters/http.js +77 -30
- package/lib/adapters/xhr.js +18 -24
- package/lib/axios.js +2 -1
- package/lib/core/Axios.js +13 -6
- package/lib/core/enhanceError.js +21 -0
- package/lib/core/mergeConfig.js +51 -0
- package/lib/core/settle.js +1 -2
- package/lib/defaults.js +10 -4
- package/lib/helpers/buildURL.js +6 -3
- package/lib/helpers/cookies.js +41 -41
- package/lib/helpers/isURLSameOrigin.js +38 -38
- package/lib/utils.js +32 -1
- package/package.json +23 -23
- package/lib/helpers/btoa.js +0 -36
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,119 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
### 0.19.0 (May 30, 2019)
|
4
|
+
|
5
|
+
Fixes and Functionality:
|
6
|
+
|
7
|
+
- Unzip response body only for statuses != 204 ([#1129](https://github.com/axios/axios/pull/1129)) - drawski
|
8
|
+
- Destroy stream on exceeding maxContentLength (fixes [#1098](https://github.com/axios/axios/issue/1098)) ([#1485](https://github.com/axios/axios/pull/1485)) - Gadzhi Gadzhiev
|
9
|
+
- Makes Axios error generic to use AxiosResponse ([#1738](https://github.com/axios/axios/pull/1738)) - Suman Lama
|
10
|
+
- Fixing Mocha tests by locking follow-redirects version to 1.5.10 ([#1993](https://github.com/axios/axios/pull/1993)) - grumblerchester
|
11
|
+
- Allow uppercase methods in typings. ([#1781](https://github.com/axios/axios/pull/1781)) - Ken Powers
|
12
|
+
- Fixing .eslintrc without extension ([#1789](https://github.com/axios/axios/pull/1789)) - Manoel
|
13
|
+
- Consistent coding style ([#1787](https://github.com/axios/axios/pull/1787)) - Ali Servet Donmez
|
14
|
+
- Fixing building url with hash mark ([#1771](https://github.com/axios/axios/pull/1771)) - Anatoly Ryabov
|
15
|
+
- This commit fix building url with hash map (fragment identifier) when parameters are present: they must not be added after `#`, because client cut everything after `#`
|
16
|
+
- Preserve HTTP method when following redirect ([#1758](https://github.com/axios/axios/pull/1758)) - Rikki Gibson
|
17
|
+
- Add `getUri` signature to TypeScript definition. ([#1736](https://github.com/axios/axios/pull/1736)) - Alexander Trauzzi
|
18
|
+
- Adding isAxiosError flag to errors thrown by axios ([#1419](https://github.com/axios/axios/pull/1419)) - Ayush Gupta
|
19
|
+
- Fix failing SauceLabs tests by updating configuration - Emily Morehouse
|
20
|
+
|
21
|
+
Documentation:
|
22
|
+
|
23
|
+
- Add information about auth parameter to README ([#2166](https://github.com/axios/axios/pull/2166)) - xlaguna
|
24
|
+
- Add DELETE to list of methods that allow data as a config option ([#2169](https://github.com/axios/axios/pull/2169)) - Daniela Borges Matos de Carvalho
|
25
|
+
- Update ECOSYSTEM.md - Add Axios Endpoints ([#2176](https://github.com/axios/axios/pull/2176)) - Renan
|
26
|
+
- Add r2curl in ECOSYSTEM ([#2141](https://github.com/axios/axios/pull/2141)) - 유용우 / CX
|
27
|
+
- Update README.md - Add instructions for installing with yarn ([#2036](https://github.com/axios/axios/pull/2036)) - Victor Hermes
|
28
|
+
- Fixing spacing for README.md ([#2066](https://github.com/axios/axios/pull/2066)) - Josh McCarty
|
29
|
+
- Update README.md. - Change `.then` to `.finally` in example code ([#2090](https://github.com/axios/axios/pull/2090)) - Omar Cai
|
30
|
+
- Clarify what values responseType can have in Node ([#2121](https://github.com/axios/axios/pull/2121)) - Tyler Breisacher
|
31
|
+
- docs(ECOSYSTEM): add axios-api-versioning ([#2020](https://github.com/axios/axios/pull/2020)) - Weffe
|
32
|
+
- It seems that `responseType: 'blob'` doesn't actually work in Node (when I tried using it, response.data was a string, not a Blob, since Node doesn't have Blobs), so this clarifies that this option should only be used in the browser
|
33
|
+
- Add issue templates - Emily Morehouse
|
34
|
+
- Update README.md. - Add Querystring library note ([#1896](https://github.com/axios/axios/pull/1896)) - Dmitriy Eroshenko
|
35
|
+
- Add react-hooks-axios to Libraries section of ECOSYSTEM.md ([#1925](https://github.com/axios/axios/pull/1925)) - Cody Chan
|
36
|
+
- Clarify in README that default timeout is 0 (no timeout) ([#1750](https://github.com/axios/axios/pull/1750)) - Ben Standefer
|
37
|
+
|
38
|
+
### 0.19.0-beta.1 (Aug 9, 2018)
|
39
|
+
|
40
|
+
**NOTE:** This is a beta version of this release. There may be functionality that is broken in
|
41
|
+
certain browsers, though we suspect that builds are hanging and not erroring. See
|
42
|
+
https://saucelabs.com/u/axios for the most up-to-date information.
|
43
|
+
|
44
|
+
New Functionality:
|
45
|
+
|
46
|
+
- Add getUri method ([#1712](https://github.com/axios/axios/issues/1712))
|
47
|
+
- Add support for no_proxy env variable ([#1693](https://github.com/axios/axios/issues/1693))
|
48
|
+
- Add toJSON to decorated Axios errors to faciliate serialization ([#1625](https://github.com/axios/axios/issues/1625))
|
49
|
+
- Add second then on axios call ([#1623](https://github.com/axios/axios/issues/1623))
|
50
|
+
- Typings: allow custom return types
|
51
|
+
- Add option to specify character set in responses (with http adapter)
|
52
|
+
|
53
|
+
Fixes:
|
54
|
+
|
55
|
+
- Fix Keep defaults local to instance ([#385](https://github.com/axios/axios/issues/385))
|
56
|
+
- Correctly catch exception in http test ([#1475](https://github.com/axios/axios/issues/1475))
|
57
|
+
- Fix accept header normalization ([#1698](https://github.com/axios/axios/issues/1698))
|
58
|
+
- Fix http adapter to allow HTTPS connections via HTTP ([#959](https://github.com/axios/axios/issues/959))
|
59
|
+
- Fix Removes usage of deprecated Buffer constructor. ([#1555](https://github.com/axios/axios/issues/1555), [#1622](https://github.com/axios/axios/issues/1622))
|
60
|
+
- Fix defaults to use httpAdapter if available ([#1285](https://github.com/axios/axios/issues/1285))
|
61
|
+
- Fixing defaults to use httpAdapter if available
|
62
|
+
- Use a safer, cross-platform method to detect the Node environment
|
63
|
+
- Fix Reject promise if request is cancelled by the browser ([#537](https://github.com/axios/axios/issues/537))
|
64
|
+
- [Typescript] Fix missing type parameters on delete/head methods
|
65
|
+
- [NS]: Send `false` flag isStandardBrowserEnv for Nativescript
|
66
|
+
- Fix missing type parameters on delete/head
|
67
|
+
- Fix Default method for an instance always overwritten by get
|
68
|
+
- Fix type error when socketPath option in AxiosRequestConfig
|
69
|
+
- Capture errors on request data streams
|
70
|
+
- Decorate resolve and reject to clear timeout in all cases
|
71
|
+
|
72
|
+
Huge thanks to everyone who contributed to this release via code (authors listed
|
73
|
+
below) or via reviews and triaging on GitHub:
|
74
|
+
|
75
|
+
- Andrew Scott <ascott18@gmail.com>
|
76
|
+
- Anthony Gauthier <antho325@hotmail.com>
|
77
|
+
- arpit <arpit2438735@gmail.com>
|
78
|
+
- ascott18
|
79
|
+
- Benedikt Rötsch <axe312ger@users.noreply.github.com>
|
80
|
+
- Chance Dickson <me@chancedickson.com>
|
81
|
+
- Dave Stewart <info@davestewart.co.uk>
|
82
|
+
- Deric Cain <deric.cain@gmail.com>
|
83
|
+
- Guillaume Briday <guillaumebriday@gmail.com>
|
84
|
+
- Jacob Wejendorp <jacob@wejendorp.dk>
|
85
|
+
- Jim Lynch <mrdotjim@gmail.com>
|
86
|
+
- johntron
|
87
|
+
- Justin Beckwith <beckwith@google.com>
|
88
|
+
- Justin Beckwith <justin.beckwith@gmail.com>
|
89
|
+
- Khaled Garbaya <khaledgarbaya@gmail.com>
|
90
|
+
- Lim Jing Rong <jjingrong@users.noreply.github.com>
|
91
|
+
- Mark van den Broek <mvdnbrk@gmail.com>
|
92
|
+
- Martti Laine <martti@codeclown.net>
|
93
|
+
- mattridley
|
94
|
+
- mattridley <matt.r@joinblink.com>
|
95
|
+
- Nicolas Del Valle <nicolas.delvalle@gmail.com>
|
96
|
+
- Nilegfx
|
97
|
+
- pbarbiero
|
98
|
+
- Rikki Gibson <rikkigibson@gmail.com>
|
99
|
+
- Sako Hartounian <sakohartounian@yahoo.com>
|
100
|
+
- Shane Fitzpatrick <fitzpasd@gmail.com>
|
101
|
+
- Stephan Schneider <stephanschndr@gmail.com>
|
102
|
+
- Steven <steven@ceriously.com>
|
103
|
+
- Tim Garthwaite <tim.garthwaite@jibo.com>
|
104
|
+
- Tim Johns <timjohns@yahoo.com>
|
105
|
+
- Yutaro Miyazaki <yutaro@studio-rubbish.com>
|
106
|
+
|
107
|
+
### 0.18.0 (Feb 19, 2018)
|
108
|
+
|
109
|
+
- Adding support for UNIX Sockets when running with Node.js ([#1070](https://github.com/axios/axios/pull/1070))
|
110
|
+
- Fixing typings ([#1177](https://github.com/axios/axios/pull/1177)):
|
111
|
+
- AxiosRequestConfig.proxy: allows type false
|
112
|
+
- AxiosProxyConfig: added auth field
|
113
|
+
- Adding function signature in AxiosInstance interface so AxiosInstance can be invoked ([#1192](https://github.com/axios/axios/pull/1192), [#1254](https://github.com/axios/axios/pull/1254))
|
114
|
+
- Allowing maxContentLength to pass through to redirected calls as maxBodyLength in follow-redirects config ([#1287](https://github.com/axios/axios/pull/1287))
|
115
|
+
- Fixing configuration when using an instance - method can now be set ([#1342](https://github.com/axios/axios/pull/1342))
|
116
|
+
|
3
117
|
### 0.17.1 (Nov 11, 2017)
|
4
118
|
|
5
119
|
- Fixing issue with web workers ([#1160](https://github.com/axios/axios/pull/1160))
|
package/LICENSE
CHANGED
package/README.md
CHANGED
@@ -3,8 +3,10 @@
|
|
3
3
|
[](https://www.npmjs.org/package/axios)
|
4
4
|
[](https://travis-ci.org/axios/axios)
|
5
5
|
[](https://coveralls.io/r/mzabriskie/axios)
|
6
|
+
[](https://packagephobia.now.sh/result?p=axios)
|
6
7
|
[](http://npm-stat.com/charts.html?package=axios)
|
7
8
|
[](https://gitter.im/mzabriskie/axios)
|
9
|
+
[](https://www.codetriage.com/axios/axios)
|
8
10
|
|
9
11
|
Promise based HTTP client for the browser and node.js
|
10
12
|
|
@@ -23,7 +25,7 @@ Promise based HTTP client for the browser and node.js
|
|
23
25
|
|
24
26
|
 |  |  |  |  |  |
|
25
27
|
--- | --- | --- | --- | --- | --- |
|
26
|
-
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
|
28
|
+
Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | 11 ✔ |
|
27
29
|
|
28
30
|
[](https://saucelabs.com/u/axios)
|
29
31
|
|
@@ -41,6 +43,12 @@ Using bower:
|
|
41
43
|
$ bower install axios
|
42
44
|
```
|
43
45
|
|
46
|
+
Using yarn:
|
47
|
+
|
48
|
+
```bash
|
49
|
+
$ yarn add axios
|
50
|
+
```
|
51
|
+
|
44
52
|
Using cdn:
|
45
53
|
|
46
54
|
```html
|
@@ -52,13 +60,20 @@ Using cdn:
|
|
52
60
|
Performing a `GET` request
|
53
61
|
|
54
62
|
```js
|
63
|
+
const axios = require('axios');
|
64
|
+
|
55
65
|
// Make a request for a user with a given ID
|
56
66
|
axios.get('/user?ID=12345')
|
57
67
|
.then(function (response) {
|
68
|
+
// handle success
|
58
69
|
console.log(response);
|
59
70
|
})
|
60
71
|
.catch(function (error) {
|
72
|
+
// handle error
|
61
73
|
console.log(error);
|
74
|
+
})
|
75
|
+
.finally(function () {
|
76
|
+
// always executed
|
62
77
|
});
|
63
78
|
|
64
79
|
// Optionally the request above could also be done as
|
@@ -72,9 +87,25 @@ axios.get('/user', {
|
|
72
87
|
})
|
73
88
|
.catch(function (error) {
|
74
89
|
console.log(error);
|
75
|
-
})
|
90
|
+
})
|
91
|
+
.then(function () {
|
92
|
+
// always executed
|
93
|
+
});
|
94
|
+
|
95
|
+
// Want to use async/await? Add the `async` keyword to your outer function/method.
|
96
|
+
async function getUser() {
|
97
|
+
try {
|
98
|
+
const response = await axios.get('/user?ID=12345');
|
99
|
+
console.log(response);
|
100
|
+
} catch (error) {
|
101
|
+
console.error(error);
|
102
|
+
}
|
103
|
+
}
|
76
104
|
```
|
77
105
|
|
106
|
+
> **NOTE:** `async/await` is part of ECMAScript 2017 and is not supported in Internet
|
107
|
+
> Explorer and older browsers, so use with caution.
|
108
|
+
|
78
109
|
Performing a `POST` request
|
79
110
|
|
80
111
|
```js
|
@@ -128,13 +159,13 @@ axios({
|
|
128
159
|
```js
|
129
160
|
// GET request for remote image
|
130
161
|
axios({
|
131
|
-
method:'get',
|
132
|
-
url:'http://bit.ly/2mTM3nY',
|
133
|
-
responseType:'stream'
|
162
|
+
method: 'get',
|
163
|
+
url: 'http://bit.ly/2mTM3nY',
|
164
|
+
responseType: 'stream'
|
134
165
|
})
|
135
|
-
.then(function(response) {
|
136
|
-
|
137
|
-
});
|
166
|
+
.then(function (response) {
|
167
|
+
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
|
168
|
+
});
|
138
169
|
```
|
139
170
|
|
140
171
|
##### axios(url[, config])
|
@@ -174,7 +205,7 @@ You can create a new instance of axios with a custom config.
|
|
174
205
|
##### axios.create([config])
|
175
206
|
|
176
207
|
```js
|
177
|
-
|
208
|
+
const instance = axios.create({
|
178
209
|
baseURL: 'https://some-domain.com/api/',
|
179
210
|
timeout: 1000,
|
180
211
|
headers: {'X-Custom-Header': 'foobar'}
|
@@ -193,6 +224,7 @@ The available instance methods are listed below. The specified config will be me
|
|
193
224
|
##### axios#post(url[, data[, config]])
|
194
225
|
##### axios#put(url[, data[, config]])
|
195
226
|
##### axios#patch(url[, data[, config]])
|
227
|
+
##### axios#getUri([config])
|
196
228
|
|
197
229
|
## Request Config
|
198
230
|
|
@@ -212,7 +244,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
212
244
|
baseURL: 'https://some-domain.com/api/',
|
213
245
|
|
214
246
|
// `transformRequest` allows changes to the request data before it is sent to the server
|
215
|
-
// This is only applicable for request methods 'PUT', 'POST', and '
|
247
|
+
// This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'
|
216
248
|
// The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
|
217
249
|
// FormData or Stream
|
218
250
|
// You may modify the headers object.
|
@@ -241,7 +273,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
241
273
|
|
242
274
|
// `paramsSerializer` is an optional function in charge of serializing `params`
|
243
275
|
// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
|
244
|
-
paramsSerializer: function(params) {
|
276
|
+
paramsSerializer: function (params) {
|
245
277
|
return Qs.stringify(params, {arrayFormat: 'brackets'})
|
246
278
|
},
|
247
279
|
|
@@ -257,7 +289,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
257
289
|
|
258
290
|
// `timeout` specifies the number of milliseconds before the request times out.
|
259
291
|
// If the request takes longer than `timeout`, the request will be aborted.
|
260
|
-
timeout: 1000,
|
292
|
+
timeout: 1000, // default is `0` (no timeout)
|
261
293
|
|
262
294
|
// `withCredentials` indicates whether or not cross-site Access-Control requests
|
263
295
|
// should be made using credentials
|
@@ -272,15 +304,22 @@ These are the available config options for making requests. Only the `url` is re
|
|
272
304
|
// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
|
273
305
|
// This will set an `Authorization` header, overwriting any existing
|
274
306
|
// `Authorization` custom headers you have set using `headers`.
|
307
|
+
// Please note that only HTTP Basic auth is configurable through this parameter.
|
308
|
+
// For Bearer tokens and such, use `Authorization` custom headers instead.
|
275
309
|
auth: {
|
276
310
|
username: 'janedoe',
|
277
311
|
password: 's00pers3cret'
|
278
312
|
},
|
279
313
|
|
280
314
|
// `responseType` indicates the type of data that the server will respond with
|
281
|
-
// options are 'arraybuffer', '
|
315
|
+
// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
|
316
|
+
// browser only: 'blob'
|
282
317
|
responseType: 'json', // default
|
283
318
|
|
319
|
+
// `responseEncoding` indicates encoding to use for decoding responses
|
320
|
+
// Note: Ignored for `responseType` of 'stream' or client-side requests
|
321
|
+
responseEncoding: 'utf8', // default
|
322
|
+
|
284
323
|
// `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
|
285
324
|
xsrfCookieName: 'XSRF-TOKEN', // default
|
286
325
|
|
@@ -297,7 +336,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
297
336
|
// Do whatever you want with the native progress event
|
298
337
|
},
|
299
338
|
|
300
|
-
// `maxContentLength` defines the max size of the http response content allowed
|
339
|
+
// `maxContentLength` defines the max size of the http response content in bytes allowed
|
301
340
|
maxContentLength: 2000,
|
302
341
|
|
303
342
|
// `validateStatus` defines whether to resolve or reject the promise for a given
|
@@ -312,13 +351,23 @@ These are the available config options for making requests. Only the `url` is re
|
|
312
351
|
// If set to 0, no redirects will be followed.
|
313
352
|
maxRedirects: 5, // default
|
314
353
|
|
354
|
+
// `socketPath` defines a UNIX Socket to be used in node.js.
|
355
|
+
// e.g. '/var/run/docker.sock' to send requests to the docker daemon.
|
356
|
+
// Only either `socketPath` or `proxy` can be specified.
|
357
|
+
// If both are specified, `socketPath` is used.
|
358
|
+
socketPath: null, // default
|
359
|
+
|
315
360
|
// `httpAgent` and `httpsAgent` define a custom agent to be used when performing http
|
316
361
|
// and https requests, respectively, in node.js. This allows options to be added like
|
317
362
|
// `keepAlive` that are not enabled by default.
|
318
363
|
httpAgent: new http.Agent({ keepAlive: true }),
|
319
364
|
httpsAgent: new https.Agent({ keepAlive: true }),
|
320
365
|
|
321
|
-
// 'proxy' defines the hostname and port of the proxy server
|
366
|
+
// 'proxy' defines the hostname and port of the proxy server.
|
367
|
+
// You can also define your proxy using the conventional `http_proxy` and
|
368
|
+
// `https_proxy` environment variables. If you are using environment variables
|
369
|
+
// for your proxy configuration, you can also define a `no_proxy` environment
|
370
|
+
// variable as a comma-separated list of domains that should not be proxied.
|
322
371
|
// Use `false` to disable proxies, ignoring environment variables.
|
323
372
|
// `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
|
324
373
|
// supplies credentials.
|
@@ -373,7 +422,7 @@ When using `then`, you will receive the response as follows:
|
|
373
422
|
|
374
423
|
```js
|
375
424
|
axios.get('/user/12345')
|
376
|
-
.then(function(response) {
|
425
|
+
.then(function (response) {
|
377
426
|
console.log(response.data);
|
378
427
|
console.log(response.status);
|
379
428
|
console.log(response.statusText);
|
@@ -400,7 +449,7 @@ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded
|
|
400
449
|
|
401
450
|
```js
|
402
451
|
// Set config defaults when creating the instance
|
403
|
-
|
452
|
+
const instance = axios.create({
|
404
453
|
baseURL: 'https://api.example.com'
|
405
454
|
});
|
406
455
|
|
@@ -410,15 +459,15 @@ instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
|
|
410
459
|
|
411
460
|
### Config order of precedence
|
412
461
|
|
413
|
-
Config will be merged with an order of precedence. The order is library defaults found in
|
462
|
+
Config will be merged with an order of precedence. The order is library defaults found in [lib/defaults.js](https://github.com/axios/axios/blob/master/lib/defaults.js#L28), 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.
|
414
463
|
|
415
464
|
```js
|
416
465
|
// Create an instance using the config defaults provided by the library
|
417
466
|
// At this point the timeout config value is `0` as is the default for the library
|
418
|
-
|
467
|
+
const instance = axios.create();
|
419
468
|
|
420
469
|
// Override timeout default for the library
|
421
|
-
// Now all requests will wait 2.5 seconds before timing out
|
470
|
+
// Now all requests using this instance will wait 2.5 seconds before timing out
|
422
471
|
instance.defaults.timeout = 2500;
|
423
472
|
|
424
473
|
// Override timeout for this request as it's known to take a long time
|
@@ -454,14 +503,14 @@ axios.interceptors.response.use(function (response) {
|
|
454
503
|
If you may need to remove an interceptor later you can.
|
455
504
|
|
456
505
|
```js
|
457
|
-
|
506
|
+
const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
|
458
507
|
axios.interceptors.request.eject(myInterceptor);
|
459
508
|
```
|
460
509
|
|
461
510
|
You can add interceptors to a custom instance of axios.
|
462
511
|
|
463
512
|
```js
|
464
|
-
|
513
|
+
const instance = axios.create();
|
465
514
|
instance.interceptors.request.use(function () {/*...*/});
|
466
515
|
```
|
467
516
|
|
@@ -508,12 +557,12 @@ You can cancel a request using a *cancel token*.
|
|
508
557
|
You can create a cancel token using the `CancelToken.source` factory as shown below:
|
509
558
|
|
510
559
|
```js
|
511
|
-
|
512
|
-
|
560
|
+
const CancelToken = axios.CancelToken;
|
561
|
+
const source = CancelToken.source();
|
513
562
|
|
514
563
|
axios.get('/user/12345', {
|
515
564
|
cancelToken: source.token
|
516
|
-
}).catch(function(thrown) {
|
565
|
+
}).catch(function (thrown) {
|
517
566
|
if (axios.isCancel(thrown)) {
|
518
567
|
console.log('Request canceled', thrown.message);
|
519
568
|
} else {
|
@@ -521,6 +570,12 @@ axios.get('/user/12345', {
|
|
521
570
|
}
|
522
571
|
});
|
523
572
|
|
573
|
+
axios.post('/user/12345', {
|
574
|
+
name: 'new name'
|
575
|
+
}, {
|
576
|
+
cancelToken: source.token
|
577
|
+
})
|
578
|
+
|
524
579
|
// cancel the request (the message parameter is optional)
|
525
580
|
source.cancel('Operation canceled by the user.');
|
526
581
|
```
|
@@ -528,8 +583,8 @@ source.cancel('Operation canceled by the user.');
|
|
528
583
|
You can also create a cancel token by passing an executor function to the `CancelToken` constructor:
|
529
584
|
|
530
585
|
```js
|
531
|
-
|
532
|
-
|
586
|
+
const CancelToken = axios.CancelToken;
|
587
|
+
let cancel;
|
533
588
|
|
534
589
|
axios.get('/user/12345', {
|
535
590
|
cancelToken: new CancelToken(function executor(c) {
|
@@ -553,7 +608,7 @@ By default, axios serializes JavaScript objects to `JSON`. To send data in the `
|
|
553
608
|
In a browser, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API as follows:
|
554
609
|
|
555
610
|
```js
|
556
|
-
|
611
|
+
const params = new URLSearchParams();
|
557
612
|
params.append('param1', 'value1');
|
558
613
|
params.append('param2', 'value2');
|
559
614
|
axios.post('/foo', params);
|
@@ -564,21 +619,38 @@ axios.post('/foo', params);
|
|
564
619
|
Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:
|
565
620
|
|
566
621
|
```js
|
567
|
-
|
622
|
+
const qs = require('qs');
|
568
623
|
axios.post('/foo', qs.stringify({ 'bar': 123 }));
|
569
624
|
```
|
570
625
|
|
626
|
+
Or in another way (ES6),
|
627
|
+
|
628
|
+
```js
|
629
|
+
import qs from 'qs';
|
630
|
+
const data = { 'bar': 123 };
|
631
|
+
const options = {
|
632
|
+
method: 'POST',
|
633
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
634
|
+
data: qs.stringify(data),
|
635
|
+
url,
|
636
|
+
};
|
637
|
+
axios(options);
|
638
|
+
```
|
639
|
+
|
571
640
|
### Node.js
|
572
641
|
|
573
642
|
In node.js, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
|
574
643
|
|
575
644
|
```js
|
576
|
-
|
645
|
+
const querystring = require('querystring');
|
577
646
|
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
|
578
647
|
```
|
579
648
|
|
580
649
|
You can also use the [`qs`](https://github.com/ljharb/qs) library.
|
581
650
|
|
651
|
+
###### NOTE
|
652
|
+
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).
|
653
|
+
|
582
654
|
## Semver
|
583
655
|
|
584
656
|
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.
|