mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Fixed merge conflicts
This commit is contained in:
@@ -23,7 +23,7 @@ Promise based HTTP client for the browser and node.js
|
||||
- [Example](#example)
|
||||
- [Axios API](#axios-api)
|
||||
- [Request method aliases](#request-method-aliases)
|
||||
- [Concurrency (Deprecated)](#concurrency-deprecated)
|
||||
- [Concurrency 👎](#concurrency-deprecated)
|
||||
- [Creating an instance](#creating-an-instance)
|
||||
- [Instance methods](#instance-methods)
|
||||
- [Request Config](#request-config)
|
||||
@@ -36,11 +36,15 @@ Promise based HTTP client for the browser and node.js
|
||||
- [Multiple Interceptors](#multiple-interceptors)
|
||||
- [Handling Errors](#handling-errors)
|
||||
- [Cancellation](#cancellation)
|
||||
- [AbortController](#abortcontroller)
|
||||
- [CancelToken 👎](#canceltoken-deprecated)
|
||||
- [Using application/x-www-form-urlencoded format](#using-applicationx-www-form-urlencoded-format)
|
||||
- [Browser](#browser)
|
||||
- [Node.js](#nodejs)
|
||||
- [Query string](#query-string)
|
||||
- [Form data](#form-data)
|
||||
- [Automatic serialization](#-automatic-serialization)
|
||||
- [Manual FormData passing](#manual-formdata-passing)
|
||||
- [Semver](#semver)
|
||||
- [Promises](#promises)
|
||||
- [TypeScript](#typescript)
|
||||
@@ -494,6 +498,11 @@ These are the available config options for making requests. Only the `url` is re
|
||||
|
||||
// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
|
||||
clarifyTimeoutError: false,
|
||||
},
|
||||
|
||||
env: {
|
||||
// The FormData class to be used to automatically serialize the payload into a FormData object
|
||||
FormData: window?.FormData || global?.FormData
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -718,10 +727,30 @@ axios.get('/user/12345')
|
||||
|
||||
## Cancellation
|
||||
|
||||
You can cancel a request using a *cancel token*.
|
||||
### AbortController
|
||||
|
||||
Starting from `v0.22.0` Axios supports AbortController to cancel requests in fetch API way:
|
||||
|
||||
```js
|
||||
const controller = new AbortController();
|
||||
|
||||
axios.get('/foo/bar', {
|
||||
signal: controller.signal
|
||||
}).then(function(response) {
|
||||
//...
|
||||
});
|
||||
// cancel the request
|
||||
controller.abort()
|
||||
```
|
||||
|
||||
### CancelToken `👎deprecated`
|
||||
|
||||
You can also cancel a request using a *CancelToken*.
|
||||
|
||||
> The axios cancel token API is based on the withdrawn [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises).
|
||||
|
||||
> This API is deprecated since v0.22.0 and shouldn't be used in new projects
|
||||
|
||||
You can create a cancel token using the `CancelToken.source` factory as shown below:
|
||||
|
||||
```js
|
||||
@@ -765,22 +794,11 @@ axios.get('/user/12345', {
|
||||
cancel();
|
||||
```
|
||||
|
||||
Axios supports AbortController to abort requests in [`fetch API`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#aborting_a_fetch) way:
|
||||
```js
|
||||
const controller = new AbortController();
|
||||
|
||||
axios.get('/foo/bar', {
|
||||
signal: controller.signal
|
||||
}).then(function(response) {
|
||||
//...
|
||||
});
|
||||
// cancel the request
|
||||
controller.abort()
|
||||
```
|
||||
|
||||
> Note: you can cancel several requests with the same cancel token/abort controller.
|
||||
> If a cancellation token is already cancelled at the moment of starting an Axios request, then the request is cancelled immediately, without any attempts to make real request.
|
||||
|
||||
> During the transition period, you can use both cancellation APIs, even for the same request:
|
||||
|
||||
## Using application/x-www-form-urlencoded format
|
||||
|
||||
By default, axios serializes JavaScript objects to `JSON`. To send data in the `application/x-www-form-urlencoded` format instead, you can use one of the following options.
|
||||
@@ -840,11 +858,75 @@ axios.post('http://something.com/', params.toString());
|
||||
|
||||
You can also use the [`qs`](https://github.com/ljharb/qs) library.
|
||||
|
||||
###### NOTE
|
||||
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).
|
||||
> NOTE:
|
||||
> The `qs` library is preferable if you need to stringify nested objects, as the `querystring` method has [known issues](https://github.com/nodejs/node-v0.x-archive/issues/1665) with that use case.
|
||||
|
||||
#### Form data
|
||||
|
||||
##### 🆕 Automatic serialization
|
||||
|
||||
Starting from `v0.27.0`, Axios supports automatic object serialization to a FormData object if the request `Content-Type`
|
||||
header is set to `multipart/form-data`.
|
||||
|
||||
The following request will submit the data in a FormData format (Browser & Node.js):
|
||||
|
||||
```js
|
||||
import axios from 'axios';
|
||||
|
||||
axios.post('https://httpbin.org/post', {x: 1}, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
}).then(({data})=> console.log(data));
|
||||
```
|
||||
|
||||
In the `node.js` build, the ([`form-data`](https://github.com/form-data/form-data)) polyfill is used by default.
|
||||
|
||||
You can overload the FormData class by setting the `env.FormData` config variable,
|
||||
but you probably won't need it in most cases:
|
||||
|
||||
```js
|
||||
const axios= require('axios');
|
||||
var FormData = require('form-data');
|
||||
|
||||
axios.post('https://httpbin.org/post', {x: 1, buf: new Buffer(10)}, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
}).then(({data})=> console.log(data));
|
||||
```
|
||||
|
||||
Axios FormData serializer supports some special endings to perform the following operations:
|
||||
|
||||
- `{}` - serialize the value with JSON.stringify
|
||||
- `[]` - unwrap the array like object as separate fields with the same key
|
||||
|
||||
```js
|
||||
const axios= require('axios');
|
||||
|
||||
axios.post('https://httpbin.org/post', {
|
||||
'myObj{}': {x: 1, s: "foo"},
|
||||
'files[]': document.querySelector('#fileInput').files
|
||||
}, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
}).then(({data})=> console.log(data));
|
||||
```
|
||||
|
||||
Axios supports the following shortcut methods: `postForm`, `putForm`, `patchForm`
|
||||
which are just the corresponding http methods with a header preset: `Content-Type`: `multipart/form-data`.
|
||||
|
||||
FileList object can be passed directly:
|
||||
|
||||
```js
|
||||
await axios.postForm('https://httpbin.org/post', document.querySelector('#fileInput').files)
|
||||
```
|
||||
|
||||
All files will be sent with the same field names: `files[]`;
|
||||
|
||||
##### Manual FormData passing
|
||||
|
||||
In node.js, you can use the [`form-data`](https://github.com/form-data/form-data) library as follows:
|
||||
|
||||
```js
|
||||
@@ -855,18 +937,7 @@ form.append('my_field', 'my value');
|
||||
form.append('my_buffer', new Buffer(10));
|
||||
form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
|
||||
|
||||
axios.post('https://example.com', form.getBuffer(), { headers: form.getHeaders() })
|
||||
```
|
||||
|
||||
Alternatively, use an interceptor:
|
||||
|
||||
```js
|
||||
axios.interceptors.request.use(config => {
|
||||
if (config.data instanceof FormData) {
|
||||
Object.assign(config.headers, config.data.getHeaders());
|
||||
}
|
||||
return config;
|
||||
});
|
||||
axios.post('https://example.com', form)
|
||||
```
|
||||
|
||||
## Semver
|
||||
|
||||
Reference in New Issue
Block a user