agent-relay 4.0.30 → 4.0.31
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.
- package/README.md +88 -48
- package/bin/agent-relay-broker-darwin-arm64 +0 -0
- package/bin/agent-relay-broker-darwin-x64 +0 -0
- package/bin/agent-relay-broker-linux-arm64 +0 -0
- package/bin/agent-relay-broker-linux-x64 +0 -0
- package/dist/index.cjs +99 -54
- package/dist/src/cli/bootstrap.d.ts +2 -1
- package/dist/src/cli/bootstrap.d.ts.map +1 -1
- package/dist/src/cli/bootstrap.js +223 -17
- package/dist/src/cli/bootstrap.js.map +1 -1
- package/dist/src/cli/commands/agent-management.d.ts.map +1 -1
- package/dist/src/cli/commands/agent-management.js +1 -3
- package/dist/src/cli/commands/agent-management.js.map +1 -1
- package/dist/src/cli/commands/auth.d.ts.map +1 -1
- package/dist/src/cli/commands/auth.js +32 -3
- package/dist/src/cli/commands/auth.js.map +1 -1
- package/dist/src/cli/commands/cloud.d.ts.map +1 -1
- package/dist/src/cli/commands/cloud.js +242 -144
- package/dist/src/cli/commands/cloud.js.map +1 -1
- package/dist/src/cli/commands/core.d.ts.map +1 -1
- package/dist/src/cli/commands/core.js +11 -6
- package/dist/src/cli/commands/core.js.map +1 -1
- package/dist/src/cli/commands/messaging.d.ts.map +1 -1
- package/dist/src/cli/commands/messaging.js +1 -3
- package/dist/src/cli/commands/messaging.js.map +1 -1
- package/dist/src/cli/commands/monitoring.d.ts.map +1 -1
- package/dist/src/cli/commands/monitoring.js +5 -4
- package/dist/src/cli/commands/monitoring.js.map +1 -1
- package/dist/src/cli/commands/on.d.ts.map +1 -1
- package/dist/src/cli/commands/on.js +1 -3
- package/dist/src/cli/commands/on.js.map +1 -1
- package/dist/src/cli/commands/setup.d.ts.map +1 -1
- package/dist/src/cli/commands/setup.js +62 -7
- package/dist/src/cli/commands/setup.js.map +1 -1
- package/dist/src/cli/commands/swarm.d.ts.map +1 -1
- package/dist/src/cli/commands/swarm.js +34 -4
- package/dist/src/cli/commands/swarm.js.map +1 -1
- package/dist/src/cli/index.js +9 -1
- package/dist/src/cli/index.js.map +1 -1
- package/dist/src/cli/lib/exit.d.ts +49 -0
- package/dist/src/cli/lib/exit.d.ts.map +1 -0
- package/dist/src/cli/lib/exit.js +73 -0
- package/dist/src/cli/lib/exit.js.map +1 -0
- package/dist/src/cli/lib/telemetry-helpers.d.ts +20 -0
- package/dist/src/cli/lib/telemetry-helpers.d.ts.map +1 -0
- package/dist/src/cli/lib/telemetry-helpers.js +31 -0
- package/dist/src/cli/lib/telemetry-helpers.js.map +1 -0
- package/node_modules/@agent-relay/cloud/package.json +2 -2
- package/node_modules/@agent-relay/config/package.json +1 -1
- package/node_modules/@agent-relay/hooks/package.json +4 -4
- package/node_modules/@agent-relay/sdk/dist/workflows/file-db.d.ts +62 -12
- package/node_modules/@agent-relay/sdk/dist/workflows/file-db.d.ts.map +1 -1
- package/node_modules/@agent-relay/sdk/dist/workflows/file-db.js +137 -47
- package/node_modules/@agent-relay/sdk/dist/workflows/file-db.js.map +1 -1
- package/node_modules/@agent-relay/sdk/package.json +2 -2
- package/node_modules/@agent-relay/telemetry/dist/client.d.ts +14 -2
- package/node_modules/@agent-relay/telemetry/dist/client.d.ts.map +1 -1
- package/node_modules/@agent-relay/telemetry/dist/client.js +22 -4
- package/node_modules/@agent-relay/telemetry/dist/client.js.map +1 -1
- package/node_modules/@agent-relay/telemetry/dist/events.d.ts +217 -10
- package/node_modules/@agent-relay/telemetry/dist/events.d.ts.map +1 -1
- package/node_modules/@agent-relay/telemetry/dist/events.js +11 -0
- package/node_modules/@agent-relay/telemetry/dist/events.js.map +1 -1
- package/node_modules/@agent-relay/telemetry/dist/index.d.ts +2 -2
- package/node_modules/@agent-relay/telemetry/dist/index.d.ts.map +1 -1
- package/node_modules/@agent-relay/telemetry/dist/index.js.map +1 -1
- package/node_modules/@agent-relay/telemetry/package.json +2 -2
- package/node_modules/@agent-relay/trajectory/package.json +2 -2
- package/node_modules/@agent-relay/user-directory/package.json +2 -2
- package/node_modules/@agent-relay/utils/package.json +2 -2
- package/node_modules/@relaycast/sdk/dist/version.d.ts +1 -1
- package/node_modules/@relaycast/sdk/dist/version.js +1 -1
- package/node_modules/@relaycast/sdk/node_modules/@relaycast/types/package.json +1 -1
- package/node_modules/@relaycast/sdk/package.json +2 -2
- package/node_modules/axios/CHANGELOG.md +166 -0
- package/node_modules/axios/README.md +210 -204
- package/node_modules/axios/dist/axios.js +92 -63
- package/node_modules/axios/dist/axios.js.map +1 -1
- package/node_modules/axios/dist/axios.min.js +2 -2
- package/node_modules/axios/dist/axios.min.js.map +1 -1
- package/node_modules/axios/dist/browser/axios.cjs +140 -101
- package/node_modules/axios/dist/browser/axios.cjs.map +1 -1
- package/node_modules/axios/dist/esm/axios.js +140 -101
- package/node_modules/axios/dist/esm/axios.js.map +1 -1
- package/node_modules/axios/dist/esm/axios.min.js +2 -2
- package/node_modules/axios/dist/esm/axios.min.js.map +1 -1
- package/node_modules/axios/dist/node/axios.cjs +199 -75
- package/node_modules/axios/dist/node/axios.cjs.map +1 -1
- package/node_modules/axios/index.d.cts +14 -28
- package/node_modules/axios/index.d.ts +132 -226
- package/node_modules/axios/lib/adapters/fetch.js +21 -6
- package/node_modules/axios/lib/adapters/http.js +88 -6
- package/node_modules/axios/lib/core/AxiosError.js +34 -33
- package/node_modules/axios/lib/core/AxiosHeaders.js +24 -25
- package/node_modules/axios/lib/core/buildFullPath.js +1 -1
- package/node_modules/axios/lib/core/mergeConfig.js +5 -3
- package/node_modules/axios/lib/defaults/index.js +13 -8
- package/node_modules/axios/lib/env/data.js +1 -1
- package/node_modules/axios/lib/helpers/AxiosURLSearchParams.js +1 -2
- package/node_modules/axios/lib/helpers/formDataToJSON.js +3 -1
- package/node_modules/axios/lib/helpers/formDataToStream.js +2 -1
- package/node_modules/axios/lib/helpers/progressEventReducer.js +5 -5
- package/node_modules/axios/lib/helpers/resolveConfig.js +11 -3
- package/node_modules/axios/lib/helpers/shouldBypassProxy.js +48 -1
- package/node_modules/axios/lib/helpers/toFormData.js +10 -2
- package/node_modules/axios/lib/utils.js +10 -10
- package/node_modules/axios/package.json +4 -4
- package/package.json +9 -9
- package/packages/cloud/package.json +2 -2
- package/packages/config/package.json +1 -1
- package/packages/hooks/package.json +4 -4
- package/packages/sdk/dist/workflows/file-db.d.ts +62 -12
- package/packages/sdk/dist/workflows/file-db.d.ts.map +1 -1
- package/packages/sdk/dist/workflows/file-db.js +137 -47
- package/packages/sdk/dist/workflows/file-db.js.map +1 -1
- package/packages/sdk/package.json +2 -2
- package/packages/telemetry/dist/client.d.ts +14 -2
- package/packages/telemetry/dist/client.d.ts.map +1 -1
- package/packages/telemetry/dist/client.js +22 -4
- package/packages/telemetry/dist/client.js.map +1 -1
- package/packages/telemetry/dist/events.d.ts +217 -10
- package/packages/telemetry/dist/events.d.ts.map +1 -1
- package/packages/telemetry/dist/events.js +11 -0
- package/packages/telemetry/dist/events.js.map +1 -1
- package/packages/telemetry/dist/index.d.ts +2 -2
- package/packages/telemetry/dist/index.d.ts.map +1 -1
- package/packages/telemetry/dist/index.js.map +1 -1
- package/packages/telemetry/package.json +2 -2
- package/packages/trajectory/package.json +2 -2
- package/packages/user-directory/package.json +2 -2
- package/packages/utils/package.json +2 -2
- package/node_modules/@clack/prompts/node_modules/is-unicode-supported/index.d.ts +0 -12
- package/node_modules/@clack/prompts/node_modules/is-unicode-supported/index.js +0 -17
- package/node_modules/@clack/prompts/node_modules/is-unicode-supported/license +0 -9
- package/node_modules/@clack/prompts/node_modules/is-unicode-supported/package.json +0 -43
- package/node_modules/@clack/prompts/node_modules/is-unicode-supported/readme.md +0 -35
- package/node_modules/color-convert/CHANGELOG.md +0 -54
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<h3 align="center"> 💎 Platinum sponsors <br> </h3> <table align="center"><tr><td align="center" width="50%"> <a href="https://thanks.dev/?utm_source=axios&utm_medium=sponsorlist&utm_campaign=sponsorship" style="padding: 10px; display: inline-block" target="_blank"> <img width="90px" height="90px" src="https://axios-http.com/assets/sponsors/opencollective/ed51c2ee8f1b70aa3484d6dd678652134079a036.png" alt="THANKS.DEV"/> </a> <p align="center" title="We're passionate about making open source sustainable. Scan your dependancy tree to better understand which open source projects need funding the most. Maintainers can also register their projects to become eligible for funding.">We're passionate about making open source sustainable. Scan your dependancy tree to better understand which open source projects need funding the...</p> <p align="center"> <a href="https://thanks.dev/?utm_source=axios&utm_medium=readme_sponsorlist&utm_campaign=sponsorship" target="_blank"><b>thanks.dev</b></a> </p>
|
|
2
|
-
</td><td align="center" width="50%"> <a href="https://opencollective.com/hopper-security?utm_source=axios&utm_medium=sponsorlist&utm_campaign=sponsorship" style="padding: 10px; display: inline-block" target="_blank"> <img width="90px" height="90px" src="https://axios-http.com/assets/sponsors/opencollective/180d02a83ee99448f850e39eed6dbb95f56000ba.png" alt="Hopper Security"/> </a> <p align="center"> </p>
|
|
2
|
+
</td><td align="center" width="50%"> <a href="https://opencollective.com/hopper-security?utm_source=axios&utm_medium=sponsorlist&utm_campaign=sponsorship" style="padding: 10px; display: inline-block" target="_blank"> <img width="90px" height="90px" src="https://axios-http.com/assets/sponsors/opencollective/180d02a83ee99448f850e39eed6dbb95f56000ba.png" alt="Hopper Security"/> </a> <p align="center">Hopper provides a secure open-source registry where every component is verified against malware and continuously remediated for vulnerabilities across any version. In simple terms, Hopper removes the need to manage software supply chain risk altogether.</p><p align="center"> <a href="https://hopper.security/?utm_source=axios&utm_medium=readme_sponsorlist&utm_campaign=sponsorship" target="_blank"><b>Hopper.Security</b></a> </p>
|
|
3
3
|
</td></tr></table><table align="center"><tr><td align="center" width="50%"> <a href="https://opencollective.com/axios/contribute" target="_blank" >💜 Become a sponsor</a>
|
|
4
4
|
</td><td align="center" width="50%"> <a href="https://opencollective.com/axios/contribute" target="_blank" >💜 Become a sponsor</a>
|
|
5
5
|
</td></tr></table>
|
|
@@ -127,12 +127,6 @@ Using npm:
|
|
|
127
127
|
$ npm install axios
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
-
Using bower:
|
|
131
|
-
|
|
132
|
-
```bash
|
|
133
|
-
$ bower install axios
|
|
134
|
-
```
|
|
135
|
-
|
|
136
130
|
Using yarn:
|
|
137
131
|
|
|
138
132
|
```bash
|
|
@@ -154,36 +148,36 @@ $ bun add axios
|
|
|
154
148
|
Once the package is installed, you can import the library using `import` or `require` approach:
|
|
155
149
|
|
|
156
150
|
```js
|
|
157
|
-
import axios, { isCancel, AxiosError } from
|
|
151
|
+
import axios, { isCancel, AxiosError } from 'axios';
|
|
158
152
|
```
|
|
159
153
|
|
|
160
154
|
You can also use the default export, since the named export is just a re-export from the Axios factory:
|
|
161
155
|
|
|
162
156
|
```js
|
|
163
|
-
import axios from
|
|
157
|
+
import axios from 'axios';
|
|
164
158
|
|
|
165
|
-
console.log(axios.isCancel(
|
|
159
|
+
console.log(axios.isCancel('something'));
|
|
166
160
|
```
|
|
167
161
|
|
|
168
162
|
If you use `require` for importing, **only the default export is available**:
|
|
169
163
|
|
|
170
164
|
```js
|
|
171
|
-
const axios = require(
|
|
165
|
+
const axios = require('axios');
|
|
172
166
|
|
|
173
|
-
console.log(axios.isCancel(
|
|
167
|
+
console.log(axios.isCancel('something'));
|
|
174
168
|
```
|
|
175
169
|
|
|
176
170
|
For some bundlers and some ES6 linters you may need to do the following:
|
|
177
171
|
|
|
178
172
|
```js
|
|
179
|
-
import { default as axios } from
|
|
173
|
+
import { default as axios } from 'axios';
|
|
180
174
|
```
|
|
181
175
|
|
|
182
176
|
For cases where something went wrong when trying to import a module into a custom or legacy environment,
|
|
183
177
|
you can try importing the module package directly:
|
|
184
178
|
|
|
185
179
|
```js
|
|
186
|
-
const axios = require(
|
|
180
|
+
const axios = require('axios/dist/browser/axios.cjs'); // browser commonJS bundle (ES2017)
|
|
187
181
|
// const axios = require('axios/dist/node/axios.cjs'); // node commonJS bundle (ES2017)
|
|
188
182
|
```
|
|
189
183
|
|
|
@@ -204,11 +198,11 @@ Using unpkg CDN:
|
|
|
204
198
|
## Example
|
|
205
199
|
|
|
206
200
|
```js
|
|
207
|
-
import axios from
|
|
201
|
+
import axios from 'axios';
|
|
208
202
|
//const axios = require('axios'); // legacy way
|
|
209
203
|
|
|
210
204
|
try {
|
|
211
|
-
const response = await axios.get(
|
|
205
|
+
const response = await axios.get('/user?ID=12345');
|
|
212
206
|
console.log(response);
|
|
213
207
|
} catch (error) {
|
|
214
208
|
console.error(error);
|
|
@@ -216,7 +210,7 @@ try {
|
|
|
216
210
|
|
|
217
211
|
// Optionally the request above could also be done as
|
|
218
212
|
axios
|
|
219
|
-
.get(
|
|
213
|
+
.get('/user', {
|
|
220
214
|
params: {
|
|
221
215
|
ID: 12345,
|
|
222
216
|
},
|
|
@@ -234,7 +228,7 @@ axios
|
|
|
234
228
|
// Want to use async/await? Add the `async` keyword to your outer function/method.
|
|
235
229
|
async function getUser() {
|
|
236
230
|
try {
|
|
237
|
-
const response = await axios.get(
|
|
231
|
+
const response = await axios.get('/user?ID=12345');
|
|
238
232
|
console.log(response);
|
|
239
233
|
} catch (error) {
|
|
240
234
|
console.error(error);
|
|
@@ -248,9 +242,9 @@ async function getUser() {
|
|
|
248
242
|
Performing a `POST` request
|
|
249
243
|
|
|
250
244
|
```js
|
|
251
|
-
const response = await axios.post(
|
|
252
|
-
firstName:
|
|
253
|
-
lastName:
|
|
245
|
+
const response = await axios.post('/user', {
|
|
246
|
+
firstName: 'Fred',
|
|
247
|
+
lastName: 'Flintstone',
|
|
254
248
|
});
|
|
255
249
|
console.log(response);
|
|
256
250
|
```
|
|
@@ -259,11 +253,11 @@ Performing multiple concurrent requests
|
|
|
259
253
|
|
|
260
254
|
```js
|
|
261
255
|
function getUserAccount() {
|
|
262
|
-
return axios.get(
|
|
256
|
+
return axios.get('/user/12345');
|
|
263
257
|
}
|
|
264
258
|
|
|
265
259
|
function getUserPermissions() {
|
|
266
|
-
return axios.get(
|
|
260
|
+
return axios.get('/user/12345/permissions');
|
|
267
261
|
}
|
|
268
262
|
|
|
269
263
|
Promise.all([getUserAccount(), getUserPermissions()]).then(function (results) {
|
|
@@ -281,11 +275,11 @@ Requests can be made by passing the relevant config to `axios`.
|
|
|
281
275
|
```js
|
|
282
276
|
// Send a POST request
|
|
283
277
|
axios({
|
|
284
|
-
method:
|
|
285
|
-
url:
|
|
278
|
+
method: 'post',
|
|
279
|
+
url: '/user/12345',
|
|
286
280
|
data: {
|
|
287
|
-
firstName:
|
|
288
|
-
lastName:
|
|
281
|
+
firstName: 'Fred',
|
|
282
|
+
lastName: 'Flintstone',
|
|
289
283
|
},
|
|
290
284
|
});
|
|
291
285
|
```
|
|
@@ -293,18 +287,18 @@ axios({
|
|
|
293
287
|
```js
|
|
294
288
|
// GET request for remote image in node.js
|
|
295
289
|
const response = await axios({
|
|
296
|
-
method:
|
|
297
|
-
url:
|
|
298
|
-
responseType:
|
|
290
|
+
method: 'get',
|
|
291
|
+
url: 'https://bit.ly/2mTM3nY',
|
|
292
|
+
responseType: 'stream',
|
|
299
293
|
});
|
|
300
|
-
response.data.pipe(fs.createWriteStream(
|
|
294
|
+
response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'));
|
|
301
295
|
```
|
|
302
296
|
|
|
303
297
|
##### axios(url[, config])
|
|
304
298
|
|
|
305
299
|
```js
|
|
306
300
|
// Send a GET request (default method)
|
|
307
|
-
axios(
|
|
301
|
+
axios('/user/12345');
|
|
308
302
|
```
|
|
309
303
|
|
|
310
304
|
### Request method aliases
|
|
@@ -348,9 +342,9 @@ You can create a new instance of axios with a custom config.
|
|
|
348
342
|
|
|
349
343
|
```js
|
|
350
344
|
const instance = axios.create({
|
|
351
|
-
baseURL:
|
|
345
|
+
baseURL: 'https://some-domain.com/api/',
|
|
352
346
|
timeout: 1000,
|
|
353
|
-
headers: {
|
|
347
|
+
headers: { 'X-Custom-Header': 'foobar' },
|
|
354
348
|
});
|
|
355
349
|
```
|
|
356
350
|
|
|
@@ -378,6 +372,19 @@ The available instance methods are listed below. The specified config will be me
|
|
|
378
372
|
|
|
379
373
|
## Request Config
|
|
380
374
|
|
|
375
|
+
### ⚠️ Security notice: decompression-bomb protection is opt-in
|
|
376
|
+
|
|
377
|
+
By default `maxContentLength` and `maxBodyLength` are `-1` (unlimited). A malicious or compromised server can return a tiny gzip/deflate/brotli body that expands to gigabytes and exhaust the Node.js process.
|
|
378
|
+
|
|
379
|
+
If you call servers you do not fully trust, **set a cap**:
|
|
380
|
+
|
|
381
|
+
```js
|
|
382
|
+
axios.defaults.maxContentLength = 10 * 1024 * 1024; // 10 MB
|
|
383
|
+
axios.defaults.maxBodyLength = 10 * 1024 * 1024;
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
See the [security guide](https://axios.rest/pages/misc/security.html) for details.
|
|
387
|
+
|
|
381
388
|
These are the available config options for making requests. Only the `url` is required. Requests will default to `GET` if `method` is not specified.
|
|
382
389
|
|
|
383
390
|
```js
|
|
@@ -436,7 +443,12 @@ These are the available config options for making requests. Only the `url` is re
|
|
|
436
443
|
serialize?: (params: Record<string, any>, options?: ParamsSerializerOptions ),
|
|
437
444
|
|
|
438
445
|
// Configuration for formatting array indexes in the params.
|
|
439
|
-
indexes: false // Three available options: (1) indexes: null (leads to no brackets), (2) (default) indexes: false (leads to empty brackets), (3) indexes: true (leads to brackets with indexes).
|
|
446
|
+
indexes: false, // Three available options: (1) indexes: null (leads to no brackets), (2) (default) indexes: false (leads to empty brackets), (3) indexes: true (leads to brackets with indexes).
|
|
447
|
+
|
|
448
|
+
// Maximum object nesting depth when serializing params. Payloads deeper than this throw an
|
|
449
|
+
// AxiosError with code ERR_FORM_DATA_DEPTH_EXCEEDED. Default: 100. Set to Infinity to disable.
|
|
450
|
+
maxDepth: 100
|
|
451
|
+
|
|
440
452
|
},
|
|
441
453
|
|
|
442
454
|
// `data` is the data to be sent as the request body
|
|
@@ -679,6 +691,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
|
679
691
|
dots: boolean; // use dots instead of brackets format
|
|
680
692
|
metaTokens: boolean; // keep special endings like {} in parameter key
|
|
681
693
|
indexes: boolean; // array indexes format null - no brackets, false - empty brackets, true - brackets with indexes
|
|
694
|
+
maxDepth: 100; // maximum object nesting depth; throws AxiosError (ERR_FORM_DATA_DEPTH_EXCEEDED) if exceeded. Set to Infinity to disable.
|
|
682
695
|
},
|
|
683
696
|
|
|
684
697
|
// http adapter only (node.js)
|
|
@@ -688,6 +701,7 @@ These are the available config options for making requests. Only the `url` is re
|
|
|
688
701
|
]
|
|
689
702
|
}
|
|
690
703
|
```
|
|
704
|
+
|
|
691
705
|
## 🔥 HTTP/2 Support
|
|
692
706
|
|
|
693
707
|
Axios has experimental HTTP/2 support available via the Node.js HTTP adapter.
|
|
@@ -731,7 +745,7 @@ The response to a request contains the following information.
|
|
|
731
745
|
When using `then`, you will receive the response as follows:
|
|
732
746
|
|
|
733
747
|
```js
|
|
734
|
-
const response = await axios.get(
|
|
748
|
+
const response = await axios.get('/user/12345');
|
|
735
749
|
console.log(response.data);
|
|
736
750
|
console.log(response.status);
|
|
737
751
|
console.log(response.statusText);
|
|
@@ -748,14 +762,13 @@ You can specify config defaults that will be applied to every request.
|
|
|
748
762
|
### Global axios defaults
|
|
749
763
|
|
|
750
764
|
```js
|
|
751
|
-
axios.defaults.baseURL =
|
|
765
|
+
axios.defaults.baseURL = 'https://api.example.com';
|
|
752
766
|
|
|
753
767
|
// Important: If axios is used with multiple domains, the AUTH_TOKEN will be sent to all of them.
|
|
754
768
|
// See below for an example using Custom instance defaults instead.
|
|
755
|
-
axios.defaults.headers.common[
|
|
769
|
+
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
|
|
756
770
|
|
|
757
|
-
axios.defaults.headers.post[
|
|
758
|
-
"application/x-www-form-urlencoded";
|
|
771
|
+
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
759
772
|
```
|
|
760
773
|
|
|
761
774
|
### Custom instance defaults
|
|
@@ -763,11 +776,11 @@ axios.defaults.headers.post["Content-Type"] =
|
|
|
763
776
|
```js
|
|
764
777
|
// Set config defaults when creating the instance
|
|
765
778
|
const instance = axios.create({
|
|
766
|
-
baseURL:
|
|
779
|
+
baseURL: 'https://api.example.com',
|
|
767
780
|
});
|
|
768
781
|
|
|
769
782
|
// Alter defaults after instance has been created
|
|
770
|
-
instance.defaults.headers.common[
|
|
783
|
+
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
|
|
771
784
|
```
|
|
772
785
|
|
|
773
786
|
### Config order of precedence
|
|
@@ -784,7 +797,7 @@ const instance = axios.create();
|
|
|
784
797
|
instance.defaults.timeout = 2500;
|
|
785
798
|
|
|
786
799
|
// Override timeout for this request as it's known to take a long time
|
|
787
|
-
instance.get(
|
|
800
|
+
instance.get('/longRequest', {
|
|
788
801
|
timeout: 5000,
|
|
789
802
|
});
|
|
790
803
|
```
|
|
@@ -806,7 +819,7 @@ instance.interceptors.request.use(
|
|
|
806
819
|
function (error) {
|
|
807
820
|
// Do something with the request error
|
|
808
821
|
return Promise.reject(error);
|
|
809
|
-
}
|
|
822
|
+
}
|
|
810
823
|
);
|
|
811
824
|
|
|
812
825
|
// Add a response interceptor
|
|
@@ -820,7 +833,7 @@ instance.interceptors.response.use(
|
|
|
820
833
|
// Any status codes that fall outside the range of 2xx cause this function to trigger
|
|
821
834
|
// Do something with response error
|
|
822
835
|
return Promise.reject(error);
|
|
823
|
-
}
|
|
836
|
+
}
|
|
824
837
|
);
|
|
825
838
|
```
|
|
826
839
|
|
|
@@ -865,11 +878,11 @@ to the options object that will tell axios to run the code synchronously and avo
|
|
|
865
878
|
```js
|
|
866
879
|
axios.interceptors.request.use(
|
|
867
880
|
function (config) {
|
|
868
|
-
config.headers.test =
|
|
881
|
+
config.headers.test = 'I am only a header!';
|
|
869
882
|
return config;
|
|
870
883
|
},
|
|
871
884
|
null,
|
|
872
|
-
{ synchronous: true }
|
|
885
|
+
{ synchronous: true }
|
|
873
886
|
);
|
|
874
887
|
```
|
|
875
888
|
|
|
@@ -881,15 +894,15 @@ asynchronous request interceptor that only needs to run at certain times.
|
|
|
881
894
|
|
|
882
895
|
```js
|
|
883
896
|
function onGetCall(config) {
|
|
884
|
-
return config.method ===
|
|
897
|
+
return config.method === 'get';
|
|
885
898
|
}
|
|
886
899
|
axios.interceptors.request.use(
|
|
887
900
|
function (config) {
|
|
888
|
-
config.headers.test =
|
|
901
|
+
config.headers.test = 'special get headers';
|
|
889
902
|
return config;
|
|
890
903
|
},
|
|
891
904
|
null,
|
|
892
|
-
{ runWhen: onGetCall }
|
|
905
|
+
{ runWhen: onGetCall }
|
|
893
906
|
);
|
|
894
907
|
```
|
|
895
908
|
|
|
@@ -913,12 +926,12 @@ const interceptor = (id) => (base) => {
|
|
|
913
926
|
return base;
|
|
914
927
|
};
|
|
915
928
|
|
|
916
|
-
instance.interceptors.request.use(interceptor(
|
|
917
|
-
instance.interceptors.request.use(interceptor(
|
|
918
|
-
instance.interceptors.request.use(interceptor(
|
|
919
|
-
instance.interceptors.response.use(interceptor(
|
|
920
|
-
instance.interceptors.response.use(interceptor(
|
|
921
|
-
instance.interceptors.response.use(interceptor(
|
|
929
|
+
instance.interceptors.request.use(interceptor('Request Interceptor 1'));
|
|
930
|
+
instance.interceptors.request.use(interceptor('Request Interceptor 2'));
|
|
931
|
+
instance.interceptors.request.use(interceptor('Request Interceptor 3'));
|
|
932
|
+
instance.interceptors.response.use(interceptor('Response Interceptor 1'));
|
|
933
|
+
instance.interceptors.response.use(interceptor('Response Interceptor 2'));
|
|
934
|
+
instance.interceptors.response.use(interceptor('Response Interceptor 3'));
|
|
922
935
|
|
|
923
936
|
// Console output:
|
|
924
937
|
// Request Interceptor 3
|
|
@@ -982,7 +995,7 @@ Below is a list of potential axios identified error:
|
|
|
982
995
|
The default behavior is to reject every response that returns with a status code that falls out of the range of 2xx and treat it as an error.
|
|
983
996
|
|
|
984
997
|
```js
|
|
985
|
-
axios.get(
|
|
998
|
+
axios.get('/user/12345').catch(function (error) {
|
|
986
999
|
if (error.response) {
|
|
987
1000
|
// The request was made and the server responded with a status code
|
|
988
1001
|
// that falls out of the range of 2xx
|
|
@@ -996,7 +1009,7 @@ axios.get("/user/12345").catch(function (error) {
|
|
|
996
1009
|
console.log(error.request);
|
|
997
1010
|
} else {
|
|
998
1011
|
// Something happened in setting up the request that triggered an Error
|
|
999
|
-
console.log(
|
|
1012
|
+
console.log('Error', error.message);
|
|
1000
1013
|
}
|
|
1001
1014
|
console.log(error.config);
|
|
1002
1015
|
});
|
|
@@ -1005,7 +1018,7 @@ axios.get("/user/12345").catch(function (error) {
|
|
|
1005
1018
|
Using the `validateStatus` config option, you can override the default condition (status >= 200 && status < 300) and define HTTP code(s) that should throw an error.
|
|
1006
1019
|
|
|
1007
1020
|
```js
|
|
1008
|
-
axios.get(
|
|
1021
|
+
axios.get('/user/12345', {
|
|
1009
1022
|
validateStatus: function (status) {
|
|
1010
1023
|
return status < 500; // Resolve only if the status code is less than 500
|
|
1011
1024
|
},
|
|
@@ -1015,7 +1028,7 @@ axios.get("/user/12345", {
|
|
|
1015
1028
|
Using `toJSON` you get an object with more information about the HTTP error.
|
|
1016
1029
|
|
|
1017
1030
|
```js
|
|
1018
|
-
axios.get(
|
|
1031
|
+
axios.get('/user/12345').catch(function (error) {
|
|
1019
1032
|
console.log(error.toJSON());
|
|
1020
1033
|
});
|
|
1021
1034
|
```
|
|
@@ -1025,7 +1038,7 @@ axios.get("/user/12345").catch(function (error) {
|
|
|
1025
1038
|
```js
|
|
1026
1039
|
async function fetchWithTimeout() {
|
|
1027
1040
|
try {
|
|
1028
|
-
const response = await axios.get(
|
|
1041
|
+
const response = await axios.get('https://example.com/data', {
|
|
1029
1042
|
timeout: 5000, // 5 seconds
|
|
1030
1043
|
transitional: {
|
|
1031
1044
|
// set to true if you prefer ETIMEDOUT over ECONNABORTED
|
|
@@ -1033,19 +1046,19 @@ async function fetchWithTimeout() {
|
|
|
1033
1046
|
},
|
|
1034
1047
|
});
|
|
1035
1048
|
|
|
1036
|
-
console.log(
|
|
1049
|
+
console.log('Response:', response.data);
|
|
1037
1050
|
} catch (error) {
|
|
1038
1051
|
if (axios.isAxiosError(error)) {
|
|
1039
|
-
if (error.code ===
|
|
1040
|
-
console.error(
|
|
1052
|
+
if (error.code === 'ECONNABORTED' || error.code === 'ETIMEDOUT') {
|
|
1053
|
+
console.error('Request timed out. Please try again.');
|
|
1041
1054
|
return;
|
|
1042
1055
|
}
|
|
1043
1056
|
|
|
1044
|
-
console.error(
|
|
1057
|
+
console.error('Axios error:', error.message);
|
|
1045
1058
|
return;
|
|
1046
1059
|
}
|
|
1047
1060
|
|
|
1048
|
-
console.error(
|
|
1061
|
+
console.error('Unexpected error:', error);
|
|
1049
1062
|
}
|
|
1050
1063
|
}
|
|
1051
1064
|
```
|
|
@@ -1060,7 +1073,7 @@ Starting from `v0.22.0` Axios supports AbortController to cancel requests in a f
|
|
|
1060
1073
|
const controller = new AbortController();
|
|
1061
1074
|
|
|
1062
1075
|
axios
|
|
1063
|
-
.get(
|
|
1076
|
+
.get('/foo/bar', {
|
|
1064
1077
|
signal: controller.signal,
|
|
1065
1078
|
})
|
|
1066
1079
|
.then(function (response) {
|
|
@@ -1085,29 +1098,29 @@ const CancelToken = axios.CancelToken;
|
|
|
1085
1098
|
const source = CancelToken.source();
|
|
1086
1099
|
|
|
1087
1100
|
axios
|
|
1088
|
-
.get(
|
|
1101
|
+
.get('/user/12345', {
|
|
1089
1102
|
cancelToken: source.token,
|
|
1090
1103
|
})
|
|
1091
1104
|
.catch(function (thrown) {
|
|
1092
1105
|
if (axios.isCancel(thrown)) {
|
|
1093
|
-
console.log(
|
|
1106
|
+
console.log('Request canceled', thrown.message);
|
|
1094
1107
|
} else {
|
|
1095
1108
|
// handle error
|
|
1096
1109
|
}
|
|
1097
1110
|
});
|
|
1098
1111
|
|
|
1099
1112
|
axios.post(
|
|
1100
|
-
|
|
1113
|
+
'/user/12345',
|
|
1101
1114
|
{
|
|
1102
|
-
name:
|
|
1115
|
+
name: 'new name',
|
|
1103
1116
|
},
|
|
1104
1117
|
{
|
|
1105
1118
|
cancelToken: source.token,
|
|
1106
|
-
}
|
|
1119
|
+
}
|
|
1107
1120
|
);
|
|
1108
1121
|
|
|
1109
1122
|
// cancel the request (the message parameter is optional)
|
|
1110
|
-
source.cancel(
|
|
1123
|
+
source.cancel('Operation canceled by the user.');
|
|
1111
1124
|
```
|
|
1112
1125
|
|
|
1113
1126
|
You can also create a cancel token by passing an executor function to the `CancelToken` constructor:
|
|
@@ -1116,7 +1129,7 @@ You can also create a cancel token by passing an executor function to the `Cance
|
|
|
1116
1129
|
const CancelToken = axios.CancelToken;
|
|
1117
1130
|
let cancel;
|
|
1118
1131
|
|
|
1119
|
-
axios.get(
|
|
1132
|
+
axios.get('/user/12345', {
|
|
1120
1133
|
cancelToken: new CancelToken(function executor(c) {
|
|
1121
1134
|
// An executor function receives a cancel function as a parameter
|
|
1122
1135
|
cancel = c;
|
|
@@ -1139,9 +1152,9 @@ cancel();
|
|
|
1139
1152
|
By default, axios serializes JavaScript objects to `JSON`. To send data in the [`application/x-www-form-urlencoded`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST) format instead, you can use the [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API, which is [supported](http://www.caniuse.com/#feat=urlsearchparams) in the vast majority of browsers, and [Node](https://nodejs.org/api/url.html#url_class_urlsearchparams) starting with v10 (released in 2018).
|
|
1140
1153
|
|
|
1141
1154
|
```js
|
|
1142
|
-
const params = new URLSearchParams({ foo:
|
|
1143
|
-
params.append(
|
|
1144
|
-
axios.post(
|
|
1155
|
+
const params = new URLSearchParams({ foo: 'bar' });
|
|
1156
|
+
params.append('extraparam', 'value');
|
|
1157
|
+
axios.post('/foo', params);
|
|
1145
1158
|
```
|
|
1146
1159
|
|
|
1147
1160
|
### Query string (Older browsers)
|
|
@@ -1151,18 +1164,18 @@ For compatibility with very old browsers, there is a [polyfill](https://github.c
|
|
|
1151
1164
|
Alternatively, you can encode data using the [`qs`](https://github.com/ljharb/qs) library:
|
|
1152
1165
|
|
|
1153
1166
|
```js
|
|
1154
|
-
const qs = require(
|
|
1155
|
-
axios.post(
|
|
1167
|
+
const qs = require('qs');
|
|
1168
|
+
axios.post('/foo', qs.stringify({ bar: 123 }));
|
|
1156
1169
|
```
|
|
1157
1170
|
|
|
1158
1171
|
Or in another way (ES6),
|
|
1159
1172
|
|
|
1160
1173
|
```js
|
|
1161
|
-
import qs from
|
|
1174
|
+
import qs from 'qs';
|
|
1162
1175
|
const data = { bar: 123 };
|
|
1163
1176
|
const options = {
|
|
1164
|
-
method:
|
|
1165
|
-
headers: {
|
|
1177
|
+
method: 'POST',
|
|
1178
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
1166
1179
|
data: qs.stringify(data),
|
|
1167
1180
|
url,
|
|
1168
1181
|
};
|
|
@@ -1174,8 +1187,8 @@ axios(options);
|
|
|
1174
1187
|
For older Node.js engines, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
|
|
1175
1188
|
|
|
1176
1189
|
```js
|
|
1177
|
-
const querystring = require(
|
|
1178
|
-
axios.post(
|
|
1190
|
+
const querystring = require('querystring');
|
|
1191
|
+
axios.post('https://something.com/', querystring.stringify({ foo: 'bar' }));
|
|
1179
1192
|
```
|
|
1180
1193
|
|
|
1181
1194
|
You can also use the [`qs`](https://github.com/ljharb/qs) library.
|
|
@@ -1192,13 +1205,13 @@ const data = {
|
|
|
1192
1205
|
arr: [1, 2, 3],
|
|
1193
1206
|
arr2: [1, [2], 3],
|
|
1194
1207
|
users: [
|
|
1195
|
-
{ name:
|
|
1196
|
-
{ name:
|
|
1208
|
+
{ name: 'Peter', surname: 'Griffin' },
|
|
1209
|
+
{ name: 'Thomas', surname: 'Anderson' },
|
|
1197
1210
|
],
|
|
1198
1211
|
};
|
|
1199
1212
|
|
|
1200
|
-
await axios.postForm(
|
|
1201
|
-
headers: {
|
|
1213
|
+
await axios.postForm('https://postman-echo.com/post', data, {
|
|
1214
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
1202
1215
|
});
|
|
1203
1216
|
```
|
|
1204
1217
|
|
|
@@ -1226,7 +1239,7 @@ const app = express();
|
|
|
1226
1239
|
|
|
1227
1240
|
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
|
|
1228
1241
|
|
|
1229
|
-
app.post(
|
|
1242
|
+
app.post('/', function (req, res, next) {
|
|
1230
1243
|
// echo body as JSON
|
|
1231
1244
|
res.send(JSON.stringify(req.body));
|
|
1232
1245
|
});
|
|
@@ -1243,22 +1256,22 @@ Setting the `Content-Type` header is not required as Axios guesses it based on t
|
|
|
1243
1256
|
|
|
1244
1257
|
```js
|
|
1245
1258
|
const formData = new FormData();
|
|
1246
|
-
formData.append(
|
|
1259
|
+
formData.append('foo', 'bar');
|
|
1247
1260
|
|
|
1248
|
-
axios.post(
|
|
1261
|
+
axios.post('https://httpbin.org/post', formData);
|
|
1249
1262
|
```
|
|
1250
1263
|
|
|
1251
1264
|
In node.js, you can use the [`form-data`](https://github.com/form-data/form-data) library as follows:
|
|
1252
1265
|
|
|
1253
1266
|
```js
|
|
1254
|
-
const FormData = require(
|
|
1267
|
+
const FormData = require('form-data');
|
|
1255
1268
|
|
|
1256
1269
|
const form = new FormData();
|
|
1257
|
-
form.append(
|
|
1258
|
-
form.append(
|
|
1259
|
-
form.append(
|
|
1270
|
+
form.append('my_field', 'my value');
|
|
1271
|
+
form.append('my_buffer', Buffer.alloc(10));
|
|
1272
|
+
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
|
|
1260
1273
|
|
|
1261
|
-
axios.post(
|
|
1274
|
+
axios.post('https://example.com', form);
|
|
1262
1275
|
```
|
|
1263
1276
|
|
|
1264
1277
|
### 🆕 Automatic serialization to FormData
|
|
@@ -1269,17 +1282,17 @@ header is set to `multipart/form-data`.
|
|
|
1269
1282
|
The following request will submit the data in a FormData format (Browser & Node.js):
|
|
1270
1283
|
|
|
1271
1284
|
```js
|
|
1272
|
-
import axios from
|
|
1285
|
+
import axios from 'axios';
|
|
1273
1286
|
|
|
1274
1287
|
axios
|
|
1275
1288
|
.post(
|
|
1276
|
-
|
|
1289
|
+
'https://httpbin.org/post',
|
|
1277
1290
|
{ x: 1 },
|
|
1278
1291
|
{
|
|
1279
1292
|
headers: {
|
|
1280
|
-
|
|
1293
|
+
'Content-Type': 'multipart/form-data',
|
|
1281
1294
|
},
|
|
1282
|
-
}
|
|
1295
|
+
}
|
|
1283
1296
|
)
|
|
1284
1297
|
.then(({ data }) => console.log(data));
|
|
1285
1298
|
```
|
|
@@ -1290,18 +1303,18 @@ You can overload the FormData class by setting the `env.FormData` config variabl
|
|
|
1290
1303
|
but you probably won't need it in most cases:
|
|
1291
1304
|
|
|
1292
1305
|
```js
|
|
1293
|
-
const axios = require(
|
|
1294
|
-
var FormData = require(
|
|
1306
|
+
const axios = require('axios');
|
|
1307
|
+
var FormData = require('form-data');
|
|
1295
1308
|
|
|
1296
1309
|
axios
|
|
1297
1310
|
.post(
|
|
1298
|
-
|
|
1311
|
+
'https://httpbin.org/post',
|
|
1299
1312
|
{ x: 1, buf: Buffer.alloc(10) },
|
|
1300
1313
|
{
|
|
1301
1314
|
headers: {
|
|
1302
|
-
|
|
1315
|
+
'Content-Type': 'multipart/form-data',
|
|
1303
1316
|
},
|
|
1304
|
-
}
|
|
1317
|
+
}
|
|
1305
1318
|
)
|
|
1306
1319
|
.then(({ data }) => console.log(data));
|
|
1307
1320
|
```
|
|
@@ -1327,6 +1340,18 @@ FormData serializer supports additional options via `config.formSerializer: obje
|
|
|
1327
1340
|
- `null` - don't add brackets (`arr: 1`, `arr: 2`, `arr: 3`)
|
|
1328
1341
|
- `false`(default) - add empty brackets (`arr[]: 1`, `arr[]: 2`, `arr[]: 3`)
|
|
1329
1342
|
- `true` - add brackets with indexes (`arr[0]: 1`, `arr[1]: 2`, `arr[2]: 3`)
|
|
1343
|
+
- `maxDepth: number = 100` - maximum object nesting depth the serializer will recurse into. If the
|
|
1344
|
+
input object exceeds this depth, an `AxiosError` with `code: 'ERR_FORM_DATA_DEPTH_EXCEEDED'` is
|
|
1345
|
+
thrown instead of overflowing the call stack. This protects server-side applications from DoS
|
|
1346
|
+
attacks via deeply nested payloads. Set to `Infinity` to disable the limit and restore pre-fix behaviour.
|
|
1347
|
+
|
|
1348
|
+
```js
|
|
1349
|
+
// Raise the limit for a schema that genuinely nests deeper than 100 levels:
|
|
1350
|
+
axios.postForm('/api', data, { formSerializer: { maxDepth: 200 } });
|
|
1351
|
+
|
|
1352
|
+
// Same protection applies to params serialization:
|
|
1353
|
+
axios.get('/api', { params: data, paramsSerializer: { maxDepth: 200 } });
|
|
1354
|
+
```
|
|
1330
1355
|
|
|
1331
1356
|
Let's say we have an object like this one:
|
|
1332
1357
|
|
|
@@ -1336,10 +1361,10 @@ const obj = {
|
|
|
1336
1361
|
arr: [1, 2, 3],
|
|
1337
1362
|
arr2: [1, [2], 3],
|
|
1338
1363
|
users: [
|
|
1339
|
-
{ name:
|
|
1340
|
-
{ name:
|
|
1364
|
+
{ name: 'Peter', surname: 'Griffin' },
|
|
1365
|
+
{ name: 'Thomas', surname: 'Anderson' },
|
|
1341
1366
|
],
|
|
1342
|
-
|
|
1367
|
+
'obj2{}': [{ x: 1 }],
|
|
1343
1368
|
};
|
|
1344
1369
|
```
|
|
1345
1370
|
|
|
@@ -1347,18 +1372,18 @@ The following steps will be executed by the Axios serializer internally:
|
|
|
1347
1372
|
|
|
1348
1373
|
```js
|
|
1349
1374
|
const formData = new FormData();
|
|
1350
|
-
formData.append(
|
|
1351
|
-
formData.append(
|
|
1352
|
-
formData.append(
|
|
1353
|
-
formData.append(
|
|
1354
|
-
formData.append(
|
|
1355
|
-
formData.append(
|
|
1356
|
-
formData.append(
|
|
1357
|
-
formData.append(
|
|
1358
|
-
formData.append(
|
|
1359
|
-
formData.append(
|
|
1360
|
-
formData.append(
|
|
1361
|
-
formData.append(
|
|
1375
|
+
formData.append('x', '1');
|
|
1376
|
+
formData.append('arr[]', '1');
|
|
1377
|
+
formData.append('arr[]', '2');
|
|
1378
|
+
formData.append('arr[]', '3');
|
|
1379
|
+
formData.append('arr2[0]', '1');
|
|
1380
|
+
formData.append('arr2[1][0]', '2');
|
|
1381
|
+
formData.append('arr2[2]', '3');
|
|
1382
|
+
formData.append('users[0][name]', 'Peter');
|
|
1383
|
+
formData.append('users[0][surname]', 'Griffin');
|
|
1384
|
+
formData.append('users[1][name]', 'Thomas');
|
|
1385
|
+
formData.append('users[1][surname]', 'Anderson');
|
|
1386
|
+
formData.append('obj2{}', '[{"x":1}]');
|
|
1362
1387
|
```
|
|
1363
1388
|
|
|
1364
1389
|
Axios supports the following shortcut methods: `postForm`, `putForm`, `patchForm`
|
|
@@ -1369,27 +1394,24 @@ which are just the corresponding http methods with the `Content-Type` header pre
|
|
|
1369
1394
|
You can easily submit a single file:
|
|
1370
1395
|
|
|
1371
1396
|
```js
|
|
1372
|
-
await axios.postForm(
|
|
1373
|
-
myVar:
|
|
1374
|
-
file: document.querySelector(
|
|
1397
|
+
await axios.postForm('https://httpbin.org/post', {
|
|
1398
|
+
myVar: 'foo',
|
|
1399
|
+
file: document.querySelector('#fileInput').files[0],
|
|
1375
1400
|
});
|
|
1376
1401
|
```
|
|
1377
1402
|
|
|
1378
1403
|
or multiple files as `multipart/form-data`:
|
|
1379
1404
|
|
|
1380
1405
|
```js
|
|
1381
|
-
await axios.postForm(
|
|
1382
|
-
|
|
1406
|
+
await axios.postForm('https://httpbin.org/post', {
|
|
1407
|
+
'files[]': document.querySelector('#fileInput').files,
|
|
1383
1408
|
});
|
|
1384
1409
|
```
|
|
1385
1410
|
|
|
1386
1411
|
`FileList` object can be passed directly:
|
|
1387
1412
|
|
|
1388
1413
|
```js
|
|
1389
|
-
await axios.postForm(
|
|
1390
|
-
"https://httpbin.org/post",
|
|
1391
|
-
document.querySelector("#fileInput").files,
|
|
1392
|
-
);
|
|
1414
|
+
await axios.postForm('https://httpbin.org/post', document.querySelector('#fileInput').files);
|
|
1393
1415
|
```
|
|
1394
1416
|
|
|
1395
1417
|
All files will be sent with the same field names: `files[]`.
|
|
@@ -1399,24 +1421,17 @@ All files will be sent with the same field names: `files[]`.
|
|
|
1399
1421
|
Pass an HTML Form element as a payload to submit it as `multipart/form-data` content.
|
|
1400
1422
|
|
|
1401
1423
|
```js
|
|
1402
|
-
await axios.postForm(
|
|
1403
|
-
"https://httpbin.org/post",
|
|
1404
|
-
document.querySelector("#htmlForm"),
|
|
1405
|
-
);
|
|
1424
|
+
await axios.postForm('https://httpbin.org/post', document.querySelector('#htmlForm'));
|
|
1406
1425
|
```
|
|
1407
1426
|
|
|
1408
1427
|
`FormData` and `HTMLForm` objects can also be posted as `JSON` by explicitly setting the `Content-Type` header to `application/json`:
|
|
1409
1428
|
|
|
1410
1429
|
```js
|
|
1411
|
-
await axios.post(
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
{
|
|
1415
|
-
headers: {
|
|
1416
|
-
"Content-Type": "application/json",
|
|
1417
|
-
},
|
|
1430
|
+
await axios.post('https://httpbin.org/post', document.querySelector('#htmlForm'), {
|
|
1431
|
+
headers: {
|
|
1432
|
+
'Content-Type': 'application/json',
|
|
1418
1433
|
},
|
|
1419
|
-
);
|
|
1434
|
+
});
|
|
1420
1435
|
```
|
|
1421
1436
|
|
|
1422
1437
|
For example, the Form
|
|
@@ -1503,7 +1518,7 @@ const { data } = await axios.post(SERVER_URL, readableStream, {
|
|
|
1503
1518
|
},
|
|
1504
1519
|
|
|
1505
1520
|
headers: {
|
|
1506
|
-
|
|
1521
|
+
'Content-Length': contentLength,
|
|
1507
1522
|
},
|
|
1508
1523
|
|
|
1509
1524
|
maxRedirects: 0, // avoid buffering the entire stream
|
|
@@ -1524,9 +1539,7 @@ Download and upload rate limits can only be set for the http adapter (node.js):
|
|
|
1524
1539
|
```js
|
|
1525
1540
|
const { data } = await axios.post(LOCAL_SERVER_URL, myBuffer, {
|
|
1526
1541
|
onUploadProgress: ({ progress, rate }) => {
|
|
1527
|
-
console.log(
|
|
1528
|
-
`Upload [${(progress * 100).toFixed(2)}%]: ${(rate / 1024).toFixed(2)}KB/s`,
|
|
1529
|
-
);
|
|
1542
|
+
console.log(`Upload [${(progress * 100).toFixed(2)}%]: ${(rate / 1024).toFixed(2)}KB/s`);
|
|
1530
1543
|
},
|
|
1531
1544
|
|
|
1532
1545
|
maxRate: [100 * 1024], // 100KB/s limit
|
|
@@ -1561,18 +1574,18 @@ The headers object is always initialized inside interceptors and transformers:
|
|
|
1561
1574
|
|
|
1562
1575
|
```ts
|
|
1563
1576
|
axios.interceptors.request.use((request: InternalAxiosRequestConfig) => {
|
|
1564
|
-
request.headers.set(
|
|
1577
|
+
request.headers.set('My-header', 'value');
|
|
1565
1578
|
|
|
1566
1579
|
request.headers.set({
|
|
1567
|
-
|
|
1568
|
-
|
|
1580
|
+
'My-set-header1': 'my-set-value1',
|
|
1581
|
+
'My-set-header2': 'my-set-value2',
|
|
1569
1582
|
});
|
|
1570
1583
|
|
|
1571
|
-
request.headers.set(
|
|
1584
|
+
request.headers.set('User-Agent', false); // disable subsequent setting the header by Axios
|
|
1572
1585
|
|
|
1573
|
-
request.headers.setContentType(
|
|
1586
|
+
request.headers.setContentType('text/plain');
|
|
1574
1587
|
|
|
1575
|
-
request.headers[
|
|
1588
|
+
request.headers['My-set-header2'] = 'newValue'; // direct access is deprecated
|
|
1576
1589
|
|
|
1577
1590
|
return request;
|
|
1578
1591
|
});
|
|
@@ -1582,9 +1595,9 @@ You can iterate over an `AxiosHeaders` instance using a `for...of` statement:
|
|
|
1582
1595
|
|
|
1583
1596
|
```js
|
|
1584
1597
|
const headers = new AxiosHeaders({
|
|
1585
|
-
foo:
|
|
1586
|
-
bar:
|
|
1587
|
-
baz:
|
|
1598
|
+
foo: '1',
|
|
1599
|
+
bar: '2',
|
|
1600
|
+
baz: '3',
|
|
1588
1601
|
});
|
|
1589
1602
|
|
|
1590
1603
|
for (const [header, value] of headers) {
|
|
@@ -1683,26 +1696,26 @@ matcher function or internal key-value parser.
|
|
|
1683
1696
|
|
|
1684
1697
|
```ts
|
|
1685
1698
|
const headers = new AxiosHeaders({
|
|
1686
|
-
|
|
1699
|
+
'Content-Type': 'multipart/form-data; boundary=Asrf456BGe4h',
|
|
1687
1700
|
});
|
|
1688
1701
|
|
|
1689
|
-
console.log(headers.get(
|
|
1702
|
+
console.log(headers.get('Content-Type'));
|
|
1690
1703
|
// multipart/form-data; boundary=Asrf456BGe4h
|
|
1691
1704
|
|
|
1692
|
-
console.log(headers.get(
|
|
1705
|
+
console.log(headers.get('Content-Type', true)); // parse key-value pairs from a string separated with \s,;= delimiters:
|
|
1693
1706
|
// [Object: null prototype] {
|
|
1694
1707
|
// 'multipart/form-data': undefined,
|
|
1695
1708
|
// boundary: 'Asrf456BGe4h'
|
|
1696
1709
|
// }
|
|
1697
1710
|
|
|
1698
1711
|
console.log(
|
|
1699
|
-
headers.get(
|
|
1700
|
-
return String(value).replace(/a/g,
|
|
1701
|
-
})
|
|
1712
|
+
headers.get('Content-Type', (value, name, headers) => {
|
|
1713
|
+
return String(value).replace(/a/g, 'ZZZ');
|
|
1714
|
+
})
|
|
1702
1715
|
);
|
|
1703
1716
|
// multipZZZrt/form-dZZZtZZZ; boundZZZry=Asrf456BGe4h
|
|
1704
1717
|
|
|
1705
|
-
console.log(headers.get(
|
|
1718
|
+
console.log(headers.get('Content-Type', /boundary=(\w+)/)?.[0]);
|
|
1706
1719
|
// boundary=Asrf456BGe4h
|
|
1707
1720
|
```
|
|
1708
1721
|
|
|
@@ -1735,9 +1748,9 @@ Unlike the `delete` method matcher, this optional matcher will be used to match
|
|
|
1735
1748
|
|
|
1736
1749
|
```ts
|
|
1737
1750
|
const headers = new AxiosHeaders({
|
|
1738
|
-
foo:
|
|
1739
|
-
|
|
1740
|
-
|
|
1751
|
+
foo: '1',
|
|
1752
|
+
'x-foo': '2',
|
|
1753
|
+
'x-bar': '3',
|
|
1741
1754
|
});
|
|
1742
1755
|
|
|
1743
1756
|
console.log(headers.clear(/^x-/)); // true
|
|
@@ -1756,11 +1769,11 @@ Set `format` to true for converting header names to lowercase and capitalizing t
|
|
|
1756
1769
|
|
|
1757
1770
|
```js
|
|
1758
1771
|
const headers = new AxiosHeaders({
|
|
1759
|
-
foo:
|
|
1772
|
+
foo: '1',
|
|
1760
1773
|
});
|
|
1761
1774
|
|
|
1762
|
-
headers.Foo =
|
|
1763
|
-
headers.FOO =
|
|
1775
|
+
headers.Foo = '2';
|
|
1776
|
+
headers.FOO = '3';
|
|
1764
1777
|
|
|
1765
1778
|
console.log(headers.toJSON()); // [Object: null prototype] { foo: '1', Foo: '2', FOO: '3' }
|
|
1766
1779
|
console.log(headers.normalize().toJSON()); // [Object: null prototype] { foo: '3' }
|
|
@@ -1827,7 +1840,7 @@ To use it by default, it must be selected explicitly:
|
|
|
1827
1840
|
|
|
1828
1841
|
```js
|
|
1829
1842
|
const { data } = axios.get(url, {
|
|
1830
|
-
adapter:
|
|
1843
|
+
adapter: 'fetch', // by default ['xhr', 'http', 'fetch']
|
|
1831
1844
|
});
|
|
1832
1845
|
```
|
|
1833
1846
|
|
|
@@ -1835,7 +1848,7 @@ You can create a separate instance for this:
|
|
|
1835
1848
|
|
|
1836
1849
|
```js
|
|
1837
1850
|
const fetchAxios = axios.create({
|
|
1838
|
-
adapter:
|
|
1851
|
+
adapter: 'fetch',
|
|
1839
1852
|
});
|
|
1840
1853
|
|
|
1841
1854
|
const { data } = fetchAxios.get(url);
|
|
@@ -1859,12 +1872,12 @@ you must disable their use inside the fetch adapter by passing null.
|
|
|
1859
1872
|
Basic example:
|
|
1860
1873
|
|
|
1861
1874
|
```js
|
|
1862
|
-
import customFetchFunction from
|
|
1875
|
+
import customFetchFunction from 'customFetchModule';
|
|
1863
1876
|
|
|
1864
1877
|
const instance = axios.create({
|
|
1865
|
-
adapter:
|
|
1878
|
+
adapter: 'fetch',
|
|
1866
1879
|
onDownloadProgress(e) {
|
|
1867
|
-
console.log(
|
|
1880
|
+
console.log('downloadProgress', e);
|
|
1868
1881
|
},
|
|
1869
1882
|
env: {
|
|
1870
1883
|
fetch: customFetchFunction,
|
|
@@ -1879,20 +1892,20 @@ const instance = axios.create({
|
|
|
1879
1892
|
A minimal example of setting up Axios for use in a [Tauri](https://tauri.app/plugin/http-client/) app with a platform fetch function that ignores CORS policy for requests.
|
|
1880
1893
|
|
|
1881
1894
|
```js
|
|
1882
|
-
import { fetch } from
|
|
1883
|
-
import axios from
|
|
1895
|
+
import { fetch } from '@tauri-apps/plugin-http';
|
|
1896
|
+
import axios from 'axios';
|
|
1884
1897
|
|
|
1885
1898
|
const instance = axios.create({
|
|
1886
|
-
adapter:
|
|
1899
|
+
adapter: 'fetch',
|
|
1887
1900
|
onDownloadProgress(e) {
|
|
1888
|
-
console.log(
|
|
1901
|
+
console.log('downloadProgress', e);
|
|
1889
1902
|
},
|
|
1890
1903
|
env: {
|
|
1891
1904
|
fetch,
|
|
1892
1905
|
},
|
|
1893
1906
|
});
|
|
1894
1907
|
|
|
1895
|
-
const { data } = await instance.get(
|
|
1908
|
+
const { data } = await instance.get('https://google.com');
|
|
1896
1909
|
```
|
|
1897
1910
|
|
|
1898
1911
|
#### 🔥 Using with SvelteKit
|
|
@@ -1902,17 +1915,14 @@ which makes it incompatible with the standard URL API. So, Axios must be configu
|
|
|
1902
1915
|
|
|
1903
1916
|
```js
|
|
1904
1917
|
export async function load({ fetch }) {
|
|
1905
|
-
const { data: post } = await axios.get(
|
|
1906
|
-
|
|
1907
|
-
{
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
Request: null,
|
|
1912
|
-
Response: null,
|
|
1913
|
-
},
|
|
1918
|
+
const { data: post } = await axios.get('https://jsonplaceholder.typicode.com/posts/1', {
|
|
1919
|
+
adapter: 'fetch',
|
|
1920
|
+
env: {
|
|
1921
|
+
fetch,
|
|
1922
|
+
Request: null,
|
|
1923
|
+
Response: null,
|
|
1914
1924
|
},
|
|
1915
|
-
);
|
|
1925
|
+
});
|
|
1916
1926
|
|
|
1917
1927
|
return { post };
|
|
1918
1928
|
}
|
|
@@ -1931,21 +1941,17 @@ Note: HTTP/2 redirects are currently not supported by the HTTP/2 adapter.
|
|
|
1931
1941
|
```js
|
|
1932
1942
|
const form = new FormData();
|
|
1933
1943
|
|
|
1934
|
-
form.append(
|
|
1944
|
+
form.append('foo', '123');
|
|
1935
1945
|
|
|
1936
|
-
const { data, headers, status } = await axios.post(
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
},
|
|
1946
|
-
responseType: "arraybuffer",
|
|
1947
|
-
}
|
|
1948
|
-
);
|
|
1946
|
+
const { data, headers, status } = await axios.post('https://httpbin.org/post', form, {
|
|
1947
|
+
onUploadProgress(e) {
|
|
1948
|
+
console.log('upload progress', e);
|
|
1949
|
+
},
|
|
1950
|
+
onDownloadProgress(e) {
|
|
1951
|
+
console.log('download progress', e);
|
|
1952
|
+
},
|
|
1953
|
+
responseType: 'arraybuffer',
|
|
1954
|
+
});
|
|
1949
1955
|
```
|
|
1950
1956
|
|
|
1951
1957
|
## Semver
|
|
@@ -1964,7 +1970,7 @@ axios includes [TypeScript](https://typescriptlang.org) definitions and a type g
|
|
|
1964
1970
|
```typescript
|
|
1965
1971
|
let user: User = null;
|
|
1966
1972
|
try {
|
|
1967
|
-
const { data } = await axios.get(
|
|
1973
|
+
const { data } = await axios.get('/user?ID=12345');
|
|
1968
1974
|
user = data.userDetails;
|
|
1969
1975
|
} catch (error) {
|
|
1970
1976
|
if (axios.isAxiosError(error)) {
|
|
@@ -1984,10 +1990,10 @@ If you use TypeScript to type check CJS JavaScript code, your only option is to
|
|
|
1984
1990
|
You can also create a custom instance with typed interceptors:
|
|
1985
1991
|
|
|
1986
1992
|
```typescript
|
|
1987
|
-
import axios, { AxiosInstance, InternalAxiosRequestConfig } from
|
|
1993
|
+
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
|
|
1988
1994
|
|
|
1989
1995
|
const apiClient: AxiosInstance = axios.create({
|
|
1990
|
-
baseURL:
|
|
1996
|
+
baseURL: 'https://api.example.com',
|
|
1991
1997
|
timeout: 10000,
|
|
1992
1998
|
});
|
|
1993
1999
|
|