You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -494,6 +498,11 @@ These are the available config options for making requests. Only the `url` is re
494
498
495
499
// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
496
500
clarifyTimeoutError:false,
501
+
},
502
+
503
+
env: {
504
+
// The FormData class to be used to automatically serialize the payload into a FormData object
505
+
FormData:window?.FormData||global?.FormData
497
506
}
498
507
}
499
508
```
@@ -718,10 +727,30 @@ axios.get('/user/12345')
718
727
719
728
## Cancellation
720
729
721
-
You can cancel a request using a *cancel token*.
730
+
### AbortController
731
+
<
F438
/div>
732
+
Starting from `v0.22.0` Axios supports AbortController to cancel requests in fetch API way:
733
+
734
+
```js
735
+
constcontroller=newAbortController();
736
+
737
+
axios.get('/foo/bar', {
738
+
signal:controller.signal
739
+
}).then(function(response) {
740
+
//...
741
+
});
742
+
// cancel the request
743
+
controller.abort()
744
+
```
745
+
746
+
### CancelToken `👎deprecated`
747
+
748
+
You can also cancel a request using a *CancelToken*.
722
749
723
750
> The axios cancel token API is based on the withdrawn [cancelable promises proposal](https://github.com/tc39/proposal-cancelable-promises).
724
751
752
+
> This API is deprecated since v0.22.0 and shouldn't be used in new projects
753
+
725
754
You can create a cancel token using the `CancelToken.source` factory as shown below:
726
755
727
756
```js
@@ -765,22 +794,11 @@ axios.get('/user/12345', {
765
794
cancel();
766
795
```
767
796
768
-
Axios supports AbortController to abort requests in [`fetch API`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#aborting_a_fetch) way:
769
-
```js
770
-
constcontroller=newAbortController();
771
-
772
-
axios.get('/foo/bar', {
773
-
signal:controller.signal
774
-
}).then(function(response) {
775
-
//...
776
-
});
777
-
// cancel the request
778
-
controller.abort()
779
-
```
780
-
781
797
> Note: you can cancel several requests with the same cancel token/abort controller.
782
798
> 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.
783
799
800
+
> During the transition period, you can use both cancellation APIs, even for the same request:
801
+
784
802
## Using application/x-www-form-urlencoded format
785
803
786
804
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.
You can also use the [`qs`](https://github.com/ljharb/qs) library.
842
860
843
-
######NOTE
844
-
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).
861
+
> NOTE:
862
+
> 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.
845
863
846
864
#### Form data
847
865
866
+
##### 🆕 Automatic serialization
867
+
868
+
Starting from `v0.27.0`, Axios supports automatic object serialization to a FormData object if the request `Content-Type`
869
+
header is set to `multipart/form-data`.
870
+
871
+
The following request will submit the data in a FormData format (Browser & Node.js):
872
+
873
+
```js
874
+
importaxiosfrom'axios';
875
+
876
+
axios.post('https://httpbin.org/post', {x:1}, {
877
+
headers: {
878
+
'Content-Type':'multipart/form-data'
879
+
}
880
+
}).then(({data})=>console.log(data));
881
+
```
882
+
883
+
In the `node.js` build, the ([`form-data`](https://github.com/form-data/form-data)) polyfill is used by default.
884
+
885
+
You can overload the FormData class by setting the `env.FormData` config variable,
0 commit comments