axios 0.18.1 → 0.19.2
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 +167 -4
- package/README.md +117 -33
- package/dist/axios.js +678 -496
- package/dist/axios.map +1 -1
- package/dist/axios.min.js +2 -8
- package/dist/axios.min.map +1 -1
- package/index.d.ts +39 -13
- package/lib/adapters/http.js +72 -31
- package/lib/adapters/xhr.js +25 -7
- package/lib/axios.js +2 -1
- package/lib/core/Axios.js +21 -6
- package/lib/core/buildFullPath.js +20 -0
- package/lib/core/dispatchRequest.js +1 -8
- package/lib/core/enhanceError.js +21 -0
- package/lib/core/mergeConfig.js +73 -0
- package/lib/core/settle.js +1 -2
- package/lib/defaults.js +2 -1
- package/lib/helpers/buildURL.js +5 -0
- package/lib/helpers/cookies.js +41 -41
- package/lib/helpers/isURLSameOrigin.js +38 -38
- package/lib/utils.js +53 -12
- package/package.json +22 -21
package/CHANGELOG.md
CHANGED
@@ -1,10 +1,173 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
### 0.
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
### 0.19.2 (Jan 20, 2020)
|
4
|
+
|
5
|
+
- Remove unnecessary XSS check ([#2679](https://github.com/axios/axios/pull/2679)) (see ([#2646](https://github.com/axios/axios/issues/2646)) for discussion)
|
6
|
+
|
7
|
+
### 0.19.1 (Jan 7, 2020)
|
8
|
+
|
9
|
+
Fixes and Functionality:
|
10
|
+
|
11
|
+
- Fixing invalid agent issue ([#1904](https://github.com/axios/axios/pull/1904))
|
12
|
+
- Fix ignore set withCredentials false ([#2582](https://github.com/axios/axios/pull/2582))
|
13
|
+
- Delete useless default to hash ([#2458](https://github.com/axios/axios/pull/2458))
|
14
|
+
- Fix HTTP/HTTPs agents passing to follow-redirect ([#1904](https://github.com/axios/axios/pull/1904))
|
15
|
+
- Fix ignore set withCredentials false ([#2582](https://github.com/axios/axios/pull/2582))
|
16
|
+
- Fix CI build failure ([#2570](https://github.com/axios/axios/pull/2570))
|
17
|
+
- Remove dependency on is-buffer from package.json ([#1816](https://github.com/axios/axios/pull/1816))
|
18
|
+
- Adding options typings ([#2341](https://github.com/axios/axios/pull/2341))
|
19
|
+
- Adding Typescript HTTP method definition for LINK and UNLINK. ([#2444](https://github.com/axios/axios/pull/2444))
|
20
|
+
- Update dist with newest changes, fixes Custom Attributes issue
|
21
|
+
- Change syntax to see if build passes ([#2488](https://github.com/axios/axios/pull/2488))
|
22
|
+
- Update Webpack + deps, remove now unnecessary polyfills ([#2410](https://github.com/axios/axios/pull/2410))
|
23
|
+
- Fix to prevent XSS, throw an error when the URL contains a JS script ([#2464](https://github.com/axios/axios/pull/2464))
|
24
|
+
- Add custom timeout error copy in config ([#2275](https://github.com/axios/axios/pull/2275))
|
25
|
+
- Add error toJSON example ([#2466](https://github.com/axios/axios/pull/2466))
|
26
|
+
- Fixing Vulnerability A Fortify Scan finds a critical Cross-Site Scrip… ([#2451](https://github.com/axios/axios/pull/2451))
|
27
|
+
- Fixing subdomain handling on no_proxy ([#2442](https://github.com/axios/axios/pull/2442))
|
28
|
+
- Make redirection from HTTP to HTTPS work ([#2426](https://github.com/axios/axios/pull/2426] and ([#2547](https://github.com/axios/axios/pull/2547))
|
29
|
+
- Add toJSON property to AxiosError type ([#2427](https://github.com/axios/axios/pull/2427))
|
30
|
+
- Fixing socket hang up error on node side for slow response. ([#1752](https://github.com/axios/axios/pull/1752))
|
31
|
+
- Alternative syntax to send data into the body ([#2317](https://github.com/axios/axios/pull/2317))
|
32
|
+
- Fixing custom config options ([#2207](https://github.com/axios/axios/pull/2207))
|
33
|
+
- Fixing set `config.method` after mergeConfig for Axios.prototype.request ([#2383](https://github.com/axios/axios/pull/2383))
|
34
|
+
- Axios create url bug ([#2290](https://github.com/axios/axios/pull/2290))
|
35
|
+
- Do not modify config.url when using a relative baseURL (resolves [#1628](https://github.com/axios/axios/issues/1098)) ([#2391](https://github.com/axios/axios/pull/2391))
|
36
|
+
- Add typescript HTTP method definition for LINK and UNLINK ([#2444](https://github.com/axios/axios/pull/2444))
|
37
|
+
|
38
|
+
Internal:
|
39
|
+
|
40
|
+
- Revert "Update Webpack + deps, remove now unnecessary polyfills" ([#2479](https://github.com/axios/axios/pull/2479))
|
41
|
+
- Order of if/else blocks is causing unit tests mocking XHR. ([#2201](https://github.com/axios/axios/pull/2201))
|
42
|
+
- Add license badge ([#2446](https://github.com/axios/axios/pull/2446))
|
43
|
+
- Fix travis CI build [#2386](https://github.com/axios/axios/pull/2386)
|
44
|
+
- Fix cancellation error on build master. #2290 #2207 ([#2407](https://github.com/axios/axios/pull/2407))
|
45
|
+
|
46
|
+
Documentation:
|
47
|
+
|
48
|
+
- Fixing typo in CHANGELOG.md: s/Functionallity/Functionality ([#2639](https://github.com/axios/axios/pull/2639))
|
49
|
+
- Fix badge, use master branch ([#2538](https://github.com/axios/axios/pull/2538))
|
50
|
+
- Fix typo in changelog [#2193](https://github.com/axios/axios/pull/2193)
|
51
|
+
- Document fix ([#2514](https://github.com/axios/axios/pull/2514))
|
52
|
+
- Update docs with no_proxy change, issue #2484 ([#2513](https://github.com/axios/axios/pull/2513))
|
53
|
+
- Fixing missing words in docs template ([#2259](https://github.com/axios/axios/pull/2259))
|
54
|
+
- 🐛Fix request finally documentation in README ([#2189](https://github.com/axios/axios/pull/2189))
|
55
|
+
- updating spelling and adding link to docs ([#2212](https://github.com/axios/axios/pull/2212))
|
56
|
+
- docs: minor tweak ([#2404](https://github.com/axios/axios/pull/2404))
|
57
|
+
- Update response interceptor docs ([#2399](https://github.com/axios/axios/pull/2399))
|
58
|
+
- Update README.md ([#2504](https://github.com/axios/axios/pull/2504))
|
59
|
+
- Fix word 'sintaxe' to 'syntax' in README.md ([#2432](https://github.com/axios/axios/pull/2432))
|
60
|
+
- upadating README: notes on CommonJS autocomplete ([#2256](https://github.com/axios/axios/pull/2256))
|
61
|
+
- Fix grammar in README.md ([#2271](https://github.com/axios/axios/pull/2271))
|
62
|
+
- Doc fixes, minor examples cleanup ([#2198](https://github.com/axios/axios/pull/2198))
|
63
|
+
|
64
|
+
### 0.19.0 (May 30, 2019)
|
65
|
+
|
66
|
+
Fixes and Functionality:
|
67
|
+
|
68
|
+
- Added support for no_proxy env variable ([#1693](https://github.com/axios/axios/pull/1693/files)) - Chance Dickson
|
69
|
+
- Unzip response body only for statuses != 204 ([#1129](https://github.com/axios/axios/pull/1129)) - drawski
|
7
70
|
- Destroy stream on exceeding maxContentLength (fixes [#1098](https://github.com/axios/axios/issues/1098)) ([#1485](https://github.com/axios/axios/pull/1485)) - Gadzhi Gadzhiev
|
71
|
+
- Makes Axios error generic to use AxiosResponse ([#1738](https://github.com/axios/axios/pull/1738)) - Suman Lama
|
72
|
+
- Fixing Mocha tests by locking follow-redirects version to 1.5.10 ([#1993](https://github.com/axios/axios/pull/1993)) - grumblerchester
|
73
|
+
- Allow uppercase methods in typings. ([#1781](https://github.com/axios/axios/pull/1781)) - Ken Powers
|
74
|
+
- Fixing building url with hash mark ([#1771](https://github.com/axios/axios/pull/1771)) - Anatoly Ryabov
|
75
|
+
- 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 `#`
|
76
|
+
- Preserve HTTP method when following redirect ([#1758](https://github.com/axios/axios/pull/1758)) - Rikki Gibson
|
77
|
+
- Add `getUri` signature to TypeScript definition. ([#1736](https://github.com/axios/axios/pull/1736)) - Alexander Trauzzi
|
78
|
+
- Adding isAxiosError flag to errors thrown by axios ([#1419](https://github.com/axios/axios/pull/1419)) - Ayush Gupta
|
79
|
+
|
80
|
+
Internal:
|
81
|
+
|
82
|
+
- Fixing .eslintrc without extension ([#1789](https://github.com/axios/axios/pull/1789)) - Manoel
|
83
|
+
- Fix failing SauceLabs tests by updating configuration - Emily Morehouse
|
84
|
+
- Add issue templates - Emily Morehouse
|
85
|
+
|
86
|
+
Documentation:
|
87
|
+
|
88
|
+
- Consistent coding style in README ([#1787](https://github.com/axios/axios/pull/1787)) - Ali Servet Donmez
|
89
|
+
- Add information about auth parameter to README ([#2166](https://github.com/axios/axios/pull/2166)) - xlaguna
|
90
|
+
- 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
|
91
|
+
- Update ECOSYSTEM.md - Add Axios Endpoints ([#2176](https://github.com/axios/axios/pull/2176)) - Renan
|
92
|
+
- Add r2curl in ECOSYSTEM ([#2141](https://github.com/axios/axios/pull/2141)) - 유용우 / CX
|
93
|
+
- Update README.md - Add instructions for installing with yarn ([#2036](https://github.com/axios/axios/pull/2036)) - Victor Hermes
|
94
|
+
- Fixing spacing for README.md ([#2066](https://github.com/axios/axios/pull/2066)) - Josh McCarty
|
95
|
+
- Update README.md. - Change `.then` to `.finally` in example code ([#2090](https://github.com/axios/axios/pull/2090)) - Omar Cai
|
96
|
+
- Clarify what values responseType can have in Node ([#2121](https://github.com/axios/axios/pull/2121)) - Tyler Breisacher
|
97
|
+
- docs(ECOSYSTEM): add axios-api-versioning ([#2020](https://github.com/axios/axios/pull/2020)) - Weffe
|
98
|
+
- 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
|
99
|
+
- Update README.md. - Add Querystring library note ([#1896](https://github.com/axios/axios/pull/1896)) - Dmitriy Eroshenko
|
100
|
+
- Add react-hooks-axios to Libraries section of ECOSYSTEM.md ([#1925](https://github.com/axios/axios/pull/1925)) - Cody Chan
|
101
|
+
- Clarify in README that default timeout is 0 (no timeout) ([#1750](https://github.com/axios/axios/pull/1750)) - Ben Standefer
|
102
|
+
|
103
|
+
### 0.19.0-beta.1 (Aug 9, 2018)
|
104
|
+
|
105
|
+
**NOTE:** This is a beta version of this release. There may be functionality that is broken in
|
106
|
+
certain browsers, though we suspect that builds are hanging and not erroring. See
|
107
|
+
https://saucelabs.com/u/axios for the most up-to-date information.
|
108
|
+
|
109
|
+
New Functionality:
|
110
|
+
|
111
|
+
- Add getUri method ([#1712](https://github.com/axios/axios/issues/1712))
|
112
|
+
- Add support for no_proxy env variable ([#1693](https://github.com/axios/axios/issues/1693))
|
113
|
+
- Add toJSON to decorated Axios errors to faciliate serialization ([#1625](https://github.com/axios/axios/issues/1625))
|
114
|
+
- Add second then on axios call ([#1623](https://github.com/axios/axios/issues/1623))
|
115
|
+
- Typings: allow custom return types
|
116
|
+
- Add option to specify character set in responses (with http adapter)
|
117
|
+
|
118
|
+
Fixes:
|
119
|
+
|
120
|
+
- Fix Keep defaults local to instance ([#385](https://github.com/axios/axios/issues/385))
|
121
|
+
- Correctly catch exception in http test ([#1475](https://github.com/axios/axios/issues/1475))
|
122
|
+
- Fix accept header normalization ([#1698](https://github.com/axios/axios/issues/1698))
|
123
|
+
- Fix http adapter to allow HTTPS connections via HTTP ([#959](https://github.com/axios/axios/issues/959))
|
124
|
+
- Fix Removes usage of deprecated Buffer constructor. ([#1555](https://github.com/axios/axios/issues/1555), [#1622](https://github.com/axios/axios/issues/1622))
|
125
|
+
- Fix defaults to use httpAdapter if available ([#1285](https://github.com/axios/axios/issues/1285))
|
126
|
+
- Fixing defaults to use httpAdapter if available
|
127
|
+
- Use a safer, cross-platform method to detect the Node environment
|
128
|
+
- Fix Reject promise if request is cancelled by the browser ([#537](https://github.com/axios/axios/issues/537))
|
129
|
+
- [Typescript] Fix missing type parameters on delete/head methods
|
130
|
+
- [NS]: Send `false` flag isStandardBrowserEnv for Nativescript
|
131
|
+
- Fix missing type parameters on delete/head
|
132
|
+
- Fix Default method for an instance always overwritten by get
|
133
|
+
- Fix type error when socketPath option in AxiosRequestConfig
|
134
|
+
- Capture errors on request data streams
|
135
|
+
- Decorate resolve and reject to clear timeout in all cases
|
136
|
+
|
137
|
+
Huge thanks to everyone who contributed to this release via code (authors listed
|
138
|
+
below) or via reviews and triaging on GitHub:
|
139
|
+
|
140
|
+
- Andrew Scott <ascott18@gmail.com>
|
141
|
+
- Anthony Gauthier <antho325@hotmail.com>
|
142
|
+
- arpit <arpit2438735@gmail.com>
|
143
|
+
- ascott18
|
144
|
+
- Benedikt Rötsch <axe312ger@users.noreply.github.com>
|
145
|
+
- Chance Dickson <me@chancedickson.com>
|
146
|
+
- Dave Stewart <info@davestewart.co.uk>
|
147
|
+
- Deric Cain <deric.cain@gmail.com>
|
148
|
+
- Guillaume Briday <guillaumebriday@gmail.com>
|
149
|
+
- Jacob Wejendorp <jacob@wejendorp.dk>
|
150
|
+
- Jim Lynch <mrdotjim@gmail.com>
|
151
|
+
- johntron
|
152
|
+
- Justin Beckwith <beckwith@google.com>
|
153
|
+
- Justin Beckwith <justin.beckwith@gmail.com>
|
154
|
+
- Khaled Garbaya <khaledgarbaya@gmail.com>
|
155
|
+
- Lim Jing Rong <jjingrong@users.noreply.github.com>
|
156
|
+
- Mark van den Broek <mvdnbrk@gmail.com>
|
157
|
+
- Martti Laine <martti@codeclown.net>
|
158
|
+
- mattridley
|
159
|
+
- mattridley <matt.r@joinblink.com>
|
160
|
+
- Nicolas Del Valle <nicolas.delvalle@gmail.com>
|
161
|
+
- Nilegfx
|
162
|
+
- pbarbiero
|
163
|
+
- Rikki Gibson <rikkigibson@gmail.com>
|
164
|
+
- Sako Hartounian <sakohartounian@yahoo.com>
|
165
|
+
- Shane Fitzpatrick <fitzpasd@gmail.com>
|
166
|
+
- Stephan Schneider <stephanschndr@gmail.com>
|
167
|
+
- Steven <steven@ceriously.com>
|
168
|
+
- Tim Garthwaite <tim.garthwaite@jibo.com>
|
169
|
+
- Tim Johns <timjohns@yahoo.com>
|
170
|
+
- Yutaro Miyazaki <yutaro@studio-rubbish.com>
|
8
171
|
|
9
172
|
### 0.18.0 (Feb 19, 2018)
|
10
173
|
|
package/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# axios
|
2
2
|
|
3
3
|
[](https://www.npmjs.org/package/axios)
|
4
|
-
[](https://travis-ci.org/axios/axios)
|
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)
|
8
9
|
[](https://www.codetriage.com/axios/axios)
|
@@ -42,6 +43,12 @@ Using bower:
|
|
42
43
|
$ bower install axios
|
43
44
|
```
|
44
45
|
|
46
|
+
Using yarn:
|
47
|
+
|
48
|
+
```bash
|
49
|
+
$ yarn add axios
|
50
|
+
```
|
51
|
+
|
45
52
|
Using cdn:
|
46
53
|
|
47
54
|
```html
|
@@ -50,16 +57,32 @@ Using cdn:
|
|
50
57
|
|
51
58
|
## Example
|
52
59
|
|
60
|
+
### note: CommonJS usage
|
61
|
+
In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with `require()` use the following approach:
|
62
|
+
|
63
|
+
```js
|
64
|
+
const axios = require('axios').default;
|
65
|
+
|
66
|
+
// axios.<method> will now provide autocomplete and parameter typings
|
67
|
+
```
|
68
|
+
|
53
69
|
Performing a `GET` request
|
54
70
|
|
55
71
|
```js
|
72
|
+
const axios = require('axios');
|
73
|
+
|
56
74
|
// Make a request for a user with a given ID
|
57
75
|
axios.get('/user?ID=12345')
|
58
76
|
.then(function (response) {
|
77
|
+
// handle success
|
59
78
|
console.log(response);
|
60
79
|
})
|
61
80
|
.catch(function (error) {
|
81
|
+
// handle error
|
62
82
|
console.log(error);
|
83
|
+
})
|
84
|
+
.finally(function () {
|
85
|
+
// always executed
|
63
86
|
});
|
64
87
|
|
65
88
|
// Optionally the request above could also be done as
|
@@ -73,9 +96,25 @@ axios.get('/user', {
|
|
73
96
|
})
|
74
97
|
.catch(function (error) {
|
75
98
|
console.log(error);
|
76
|
-
})
|
99
|
+
})
|
100
|
+
.finally(function () {
|
101
|
+
// always executed
|
102
|
+
});
|
103
|
+
|
104
|
+
// Want to use async/await? Add the `async` keyword to your outer function/method.
|
105
|
+
async function getUser() {
|
106
|
+
try {
|
107
|
+
const response = await axios.get('/user?ID=12345');
|
108
|
+
console.log(response);
|
109
|
+
} catch (error) {
|
110
|
+
console.error(error);
|
111
|
+
}
|
112
|
+
}
|
77
113
|
```
|
78
114
|
|
115
|
+
> **NOTE:** `async/await` is part of ECMAScript 2017 and is not supported in Internet
|
116
|
+
> Explorer and older browsers, so use with caution.
|
117
|
+
|
79
118
|
Performing a `POST` request
|
80
119
|
|
81
120
|
```js
|
@@ -129,13 +168,13 @@ axios({
|
|
129
168
|
```js
|
130
169
|
// GET request for remote image
|
131
170
|
axios({
|
132
|
-
method:'get',
|
133
|
-
url:'http://bit.ly/2mTM3nY',
|
134
|
-
responseType:'stream'
|
171
|
+
method: 'get',
|
172
|
+
url: 'http://bit.ly/2mTM3nY',
|
173
|
+
responseType: 'stream'
|
135
174
|
})
|
136
|
-
.then(function(response) {
|
137
|
-
|
138
|
-
});
|
175
|
+
.then(function (response) {
|
176
|
+
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))
|
177
|
+
});
|
139
178
|
```
|
140
179
|
|
141
180
|
##### axios(url[, config])
|
@@ -175,7 +214,7 @@ You can create a new instance of axios with a custom config.
|
|
175
214
|
##### axios.create([config])
|
176
215
|
|
177
216
|
```js
|
178
|
-
|
217
|
+
const instance = axios.create({
|
179
218
|
baseURL: 'https://some-domain.com/api/',
|
180
219
|
timeout: 1000,
|
181
220
|
headers: {'X-Custom-Header': 'foobar'}
|
@@ -194,6 +233,7 @@ The available instance methods are listed below. The specified config will be me
|
|
194
233
|
##### axios#post(url[, data[, config]])
|
195
234
|
##### axios#put(url[, data[, config]])
|
196
235
|
##### axios#patch(url[, data[, config]])
|
236
|
+
##### axios#getUri([config])
|
197
237
|
|
198
238
|
## Request Config
|
199
239
|
|
@@ -213,7 +253,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
213
253
|
baseURL: 'https://some-domain.com/api/',
|
214
254
|
|
215
255
|
// `transformRequest` allows changes to the request data before it is sent to the server
|
216
|
-
// This is only applicable for request methods 'PUT', 'POST', and '
|
256
|
+
// This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'
|
217
257
|
// The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
|
218
258
|
// FormData or Stream
|
219
259
|
// You may modify the headers object.
|
@@ -242,7 +282,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
242
282
|
|
243
283
|
// `paramsSerializer` is an optional function in charge of serializing `params`
|
244
284
|
// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
|
245
|
-
paramsSerializer: function(params) {
|
285
|
+
paramsSerializer: function (params) {
|
246
286
|
return Qs.stringify(params, {arrayFormat: 'brackets'})
|
247
287
|
},
|
248
288
|
|
@@ -255,10 +295,15 @@ These are the available config options for making requests. Only the `url` is re
|
|
255
295
|
data: {
|
256
296
|
firstName: 'Fred'
|
257
297
|
},
|
298
|
+
|
299
|
+
// syntax alternative to send data into the body
|
300
|
+
// method post
|
301
|
+
// only the value is sent, not the key
|
302
|
+
data: 'Country=Brasil&City=Belo Horizonte',
|
258
303
|
|
259
304
|
// `timeout` specifies the number of milliseconds before the request times out.
|
260
305
|
// If the request takes longer than `timeout`, the request will be aborted.
|
261
|
-
timeout: 1000,
|
306
|
+
timeout: 1000, // default is `0` (no timeout)
|
262
307
|
|
263
308
|
// `withCredentials` indicates whether or not cross-site Access-Control requests
|
264
309
|
// should be made using credentials
|
@@ -273,15 +318,22 @@ These are the available config options for making requests. Only the `url` is re
|
|
273
318
|
// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
|
274
319
|
// This will set an `Authorization` header, overwriting any existing
|
275
320
|
// `Authorization` custom headers you have set using `headers`.
|
321
|
+
// Please note that only HTTP Basic auth is configurable through this parameter.
|
322
|
+
// For Bearer tokens and such, use `Authorization` custom headers instead.
|
276
323
|
auth: {
|
277
324
|
username: 'janedoe',
|
278
325
|
password: 's00pers3cret'
|
279
326
|
},
|
280
327
|
|
281
328
|
// `responseType` indicates the type of data that the server will respond with
|
282
|
-
// options are 'arraybuffer', '
|
329
|
+
// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
|
330
|
+
// browser only: 'blob'
|
283
331
|
responseType: 'json', // default
|
284
332
|
|
333
|
+
// `responseEncoding` indicates encoding to use for decoding responses
|
334
|
+
// Note: Ignored for `responseType` of 'stream' or client-side requests
|
335
|
+
responseEncoding: 'utf8', // default
|
336
|
+
|
285
337
|
// `xsrfCookieName` is the name of the cookie to use as a value for xsrf token
|
286
338
|
xsrfCookieName: 'XSRF-TOKEN', // default
|
287
339
|
|
@@ -298,7 +350,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
298
350
|
// Do whatever you want with the native progress event
|
299
351
|
},
|
300
352
|
|
301
|
-
// `maxContentLength` defines the max size of the http response content allowed
|
353
|
+
// `maxContentLength` defines the max size of the http response content in bytes allowed
|
302
354
|
maxContentLength: 2000,
|
303
355
|
|
304
356
|
// `validateStatus` defines whether to resolve or reject the promise for a given
|
@@ -325,7 +377,11 @@ These are the available config options for making requests. Only the `url` is re
|
|
325
377
|
httpAgent: new http.Agent({ keepAlive: true }),
|
326
378
|
httpsAgent: new https.Agent({ keepAlive: true }),
|
327
379
|
|
328
|
-
// 'proxy' defines the hostname and port of the proxy server
|
380
|
+
// 'proxy' defines the hostname and port of the proxy server.
|
381
|
+
// You can also define your proxy using the conventional `http_proxy` and
|
382
|
+
// `https_proxy` environment variables. If you are using environment variables
|
383
|
+
// for your proxy configuration, you can also define a `no_proxy` environment
|
384
|
+
// variable as a comma-separated list of domains that should not be proxied.
|
329
385
|
// Use `false` to disable proxies, ignoring environment variables.
|
330
386
|
// `auth` indicates that HTTP Basic auth should be used to connect to the proxy, and
|
331
387
|
// supplies credentials.
|
@@ -371,7 +427,7 @@ The response for a request contains the following information.
|
|
371
427
|
|
372
428
|
// `request` is the request that generated this response
|
373
429
|
// It is the last ClientRequest instance in node.js (in redirects)
|
374
|
-
// and an XMLHttpRequest instance the browser
|
430
|
+
// and an XMLHttpRequest instance in the browser
|
375
431
|
request: {}
|
376
432
|
}
|
377
433
|
```
|
@@ -380,7 +436,7 @@ When using `then`, you will receive the response as follows:
|
|
380
436
|
|
381
437
|
```js
|
382
438
|
axios.get('/user/12345')
|
383
|
-
.then(function(response) {
|
439
|
+
.then(function (response) {
|
384
440
|
console.log(response.data);
|
385
441
|
console.log(response.status);
|
386
442
|
console.log(response.statusText);
|
@@ -407,7 +463,7 @@ axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded
|
|
407
463
|
|
408
464
|
```js
|
409
465
|
// Set config defaults when creating the instance
|
410
|
-
|
466
|
+
const instance = axios.create({
|
411
467
|
baseURL: 'https://api.example.com'
|
412
468
|
});
|
413
469
|
|
@@ -422,10 +478,10 @@ Config will be merged with an order of precedence. The order is library defaults
|
|
422
478
|
```js
|
423
479
|
// Create an instance using the config defaults provided by the library
|
424
480
|
// At this point the timeout config value is `0` as is the default for the library
|
425
|
-
|
481
|
+
const instance = axios.create();
|
426
482
|
|
427
483
|
// Override timeout default for the library
|
428
|
-
// Now all requests will wait 2.5 seconds before timing out
|
484
|
+
// Now all requests using this instance will wait 2.5 seconds before timing out
|
429
485
|
instance.defaults.timeout = 2500;
|
430
486
|
|
431
487
|
// Override timeout for this request as it's known to take a long time
|
@@ -450,25 +506,27 @@ axios.interceptors.request.use(function (config) {
|
|
450
506
|
|
451
507
|
// Add a response interceptor
|
452
508
|
axios.interceptors.response.use(function (response) {
|
509
|
+
// Any status code that lie within the range of 2xx cause this function to trigger
|
453
510
|
// Do something with response data
|
454
511
|
return response;
|
455
512
|
}, function (error) {
|
513
|
+
// Any status codes that falls outside the range of 2xx cause this function to trigger
|
456
514
|
// Do something with response error
|
457
515
|
return Promise.reject(error);
|
458
516
|
});
|
459
517
|
```
|
460
518
|
|
461
|
-
If you
|
519
|
+
If you need to remove an interceptor later you can.
|
462
520
|
|
463
521
|
```js
|
464
|
-
|
522
|
+
const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
|
465
523
|
axios.interceptors.request.eject(myInterceptor);
|
466
524
|
```
|
467
525
|
|
468
526
|
You can add interceptors to a custom instance of axios.
|
469
527
|
|
470
528
|
```js
|
471
|
-
|
529
|
+
const instance = axios.create();
|
472
530
|
instance.interceptors.request.use(function () {/*...*/});
|
473
531
|
```
|
474
532
|
|
@@ -496,7 +554,7 @@ axios.get('/user/12345')
|
|
496
554
|
});
|
497
555
|
```
|
498
556
|
|
499
|
-
|
557
|
+
Using the `validateStatus` config option, you can define HTTP code(s) that should throw an error.
|
500
558
|
|
501
559
|
```js
|
502
560
|
axios.get('/user/12345', {
|
@@ -506,6 +564,15 @@ axios.get('/user/12345', {
|
|
506
564
|
})
|
507
565
|
```
|
508
566
|
|
567
|
+
Using `toJSON` you get an object with more information about the HTTP error.
|
568
|
+
|
569
|
+
```js
|
570
|
+
axios.get('/user/12345')
|
571
|
+
.catch(function (error) {
|
572
|
+
console.log(error.toJSON());
|
573
|
+
});
|
574
|
+
```
|
575
|
+
|
509
576
|
## Cancellation
|
510
577
|
|
511
578
|
You can cancel a request using a *cancel token*.
|
@@ -515,12 +582,12 @@ You can cancel a request using a *cancel token*.
|
|
515
582
|
You can create a cancel token using the `CancelToken.source` factory as shown below:
|
516
583
|
|
517
584
|
```js
|
518
|
-
|
519
|
-
|
585
|
+
const CancelToken = axios.CancelToken;
|
586
|
+
const source = CancelToken.source();
|
520
587
|
|
521
588
|
axios.get('/user/12345', {
|
522
589
|
cancelToken: source.token
|
523
|
-
}).catch(function(thrown) {
|
590
|
+
}).catch(function (thrown) {
|
524
591
|
if (axios.isCancel(thrown)) {
|
525
592
|
console.log('Request canceled', thrown.message);
|
526
593
|
} else {
|
@@ -541,8 +608,8 @@ source.cancel('Operation canceled by the user.');
|
|
541
608
|
You can also create a cancel token by passing an executor function to the `CancelToken` constructor:
|
542
609
|
|
543
610
|
```js
|
544
|
-
|
545
|
-
|
611
|
+
const CancelToken = axios.CancelToken;
|
612
|
+
let cancel;
|
546
613
|
|
547
614
|
axios.get('/user/12345', {
|
548
615
|
cancelToken: new CancelToken(function executor(c) {
|
@@ -566,7 +633,7 @@ By default, axios serializes JavaScript objects to `JSON`. To send data in the `
|
|
566
633
|
In a browser, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API as follows:
|
567
634
|
|
568
635
|
```js
|
569
|
-
|
636
|
+
const params = new URLSearchParams();
|
570
637
|
params.append('param1', 'value1');
|
571
638
|
params.append('param2', 'value2');
|
572
639
|
axios.post('/foo', params);
|
@@ -577,21 +644,38 @@ axios.post('/foo', params);
|
|
577
644
|
Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:
|
578
645
|
|
579
646
|
```js
|
580
|
-
|
647
|
+
const qs = require('qs');
|
581
648
|
axios.post('/foo', qs.stringify({ 'bar': 123 }));
|
582
649
|
```
|
583
650
|
|
651
|
+
Or in another way (ES6),
|
652
|
+
|
653
|
+
```js
|
654
|
+
import qs from 'qs';
|
655
|
+
const data = { 'bar': 123 };
|
656
|
+
const options = {
|
657
|
+
method: 'POST',
|
658
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
659
|
+
data: qs.stringify(data),
|
660
|
+
url,
|
661
|
+
};
|
662
|
+
axios(options);
|
663
|
+
```
|
664
|
+
|
584
665
|
### Node.js
|
585
666
|
|
586
667
|
In node.js, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
|
587
668
|
|
588
669
|
```js
|
589
|
-
|
670
|
+
const querystring = require('querystring');
|
590
671
|
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
|
591
672
|
```
|
592
673
|
|
593
674
|
You can also use the [`qs`](https://github.com/ljharb/qs) library.
|
594
675
|
|
676
|
+
###### NOTE
|
677
|
+
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).
|
678
|
+
|
595
679
|
## Semver
|
596
680
|
|
597
681
|
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.
|
@@ -622,4 +706,4 @@ axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/
|
|
622
706
|
|
623
707
|
## License
|
624
708
|
|
625
|
-
MIT
|
709
|
+
[MIT](LICENSE)
|