mirror of
https://git.mirrors.martin98.com/https://github.com/cyberman54/curl
synced 2026-03-31 23:43:15 +08:00
fix: validate status and some security
This commit is contained in:
250
node_modules/axios/README.md
generated
vendored
250
node_modules/axios/README.md
generated
vendored
@@ -1,15 +1,53 @@
|
||||
# axios
|
||||
|
||||
[](https://www.npmjs.org/package/axios)
|
||||
[](https://travis-ci.org/axios/axios)
|
||||
[](https://cdnjs.com/libraries/axios)
|
||||

|
||||
[](https://gitpod.io/#https://github.com/axios/axios)
|
||||
[](https://coveralls.io/r/mzabriskie/axios)
|
||||
[](https://packagephobia.now.sh/result?p=axios)
|
||||
[](http://npm-stat.com/charts.html?package=axios)
|
||||
[](https://gitter.im/mzabriskie/axios)
|
||||
[](https://www.codetriage.com/axios/axios)
|
||||
[](https://snyk.io/test/npm/axios)
|
||||
|
||||
Promise based HTTP client for the browser and node.js
|
||||
|
||||
> New axios docs website: [click here](https://axios-http.com/)
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Features](#features)
|
||||
- [Browser Support](#browser-support)
|
||||
- [Installing](#installing)
|
||||
- [Example](#example)
|
||||
- [Axios API](#axios-api)
|
||||
- [Request method aliases](#request-method-aliases)
|
||||
- [Concurrency (Deprecated)](#concurrency-deprecated)
|
||||
- [Creating an instance](#creating-an-instance)
|
||||
- [Instance methods](#instance-methods)
|
||||
- [Request Config](#request-config)
|
||||
- [Response Schema](#response-schema)
|
||||
- [Config Defaults](#config-defaults)
|
||||
- [Global axios defaults](#global-axios-defaults)
|
||||
- [Custom instance defaults](#custom-instance-defaults)
|
||||
- [Config order of precedence](#config-order-of-precedence)
|
||||
- [Interceptors](#interceptors)
|
||||
- [Multiple Interceptors](#multiple-interceptors)
|
||||
- [Handling Errors](#handling-errors)
|
||||
- [Cancellation](#cancellation)
|
||||
- [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)
|
||||
- [Semver](#semver)
|
||||
- [Promises](#promises)
|
||||
- [TypeScript](#typescript)
|
||||
- [Resources](#resources)
|
||||
- [Credits](#credits)
|
||||
- [License](#license)
|
||||
|
||||
## Features
|
||||
|
||||
- Make [XMLHttpRequests](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) from the browser
|
||||
@@ -49,7 +87,13 @@ Using yarn:
|
||||
$ yarn add axios
|
||||
```
|
||||
|
||||
Using cdn:
|
||||
Using jsDelivr CDN:
|
||||
|
||||
```html
|
||||
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
|
||||
```
|
||||
|
||||
Using unpkg CDN:
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
|
||||
@@ -81,7 +125,7 @@ axios.get('/user?ID=12345')
|
||||
// handle error
|
||||
console.log(error);
|
||||
})
|
||||
.finally(function () {
|
||||
.then(function () {
|
||||
// always executed
|
||||
});
|
||||
|
||||
@@ -97,7 +141,7 @@ axios.get('/user', {
|
||||
.catch(function (error) {
|
||||
console.log(error);
|
||||
})
|
||||
.finally(function () {
|
||||
.then(function () {
|
||||
// always executed
|
||||
});
|
||||
|
||||
@@ -141,10 +185,11 @@ function getUserPermissions() {
|
||||
return axios.get('/user/12345/permissions');
|
||||
}
|
||||
|
||||
axios.all([getUserAccount(), getUserPermissions()])
|
||||
.then(axios.spread(function (acct, perms) {
|
||||
// Both requests are now complete
|
||||
}));
|
||||
Promise.all([getUserAccount(), getUserPermissions()])
|
||||
.then(function (results) {
|
||||
const acct = results[0];
|
||||
const perm = results[1];
|
||||
});
|
||||
```
|
||||
|
||||
## axios API
|
||||
@@ -166,7 +211,7 @@ axios({
|
||||
```
|
||||
|
||||
```js
|
||||
// GET request for remote image
|
||||
// GET request for remote image in node.js
|
||||
axios({
|
||||
method: 'get',
|
||||
url: 'http://bit.ly/2mTM3nY',
|
||||
@@ -200,12 +245,13 @@ For convenience aliases have been provided for all supported request methods.
|
||||
###### NOTE
|
||||
When using the alias methods `url`, `method`, and `data` properties don't need to be specified in config.
|
||||
|
||||
### Concurrency
|
||||
### Concurrency (Deprecated)
|
||||
Please use `Promise.all` to replace the below functions.
|
||||
|
||||
Helper functions for dealing with concurrent requests.
|
||||
|
||||
##### axios.all(iterable)
|
||||
##### axios.spread(callback)
|
||||
axios.all(iterable)
|
||||
axios.spread(callback)
|
||||
|
||||
### Creating an instance
|
||||
|
||||
@@ -287,7 +333,7 @@ These are the available config options for making requests. Only the `url` is re
|
||||
},
|
||||
|
||||
// `data` is the data to be sent as the request body
|
||||
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||
// Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'
|
||||
// When no `transformRequest` is set, must be of one of the following types:
|
||||
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
|
||||
// - Browser only: FormData, File, Blob
|
||||
@@ -330,7 +376,7 @@ These are the available config options for making requests. Only the `url` is re
|
||||
// browser only: 'blob'
|
||||
responseType: 'json', // default
|
||||
|
||||
// `responseEncoding` indicates encoding to use for decoding responses
|
||||
// `responseEncoding` indicates encoding to use for decoding responses (Node.js only)
|
||||
// Note: Ignored for `responseType` of 'stream' or client-side requests
|
||||
responseEncoding: 'utf8', // default
|
||||
|
||||
@@ -341,18 +387,23 @@ These are the available config options for making requests. Only the `url` is re
|
||||
xsrfHeaderName: 'X-XSRF-TOKEN', // default
|
||||
|
||||
// `onUploadProgress` allows handling of progress events for uploads
|
||||
// browser only
|
||||
onUploadProgress: function (progressEvent) {
|
||||
// Do whatever you want with the native progress event
|
||||
},
|
||||
|
||||
// `onDownloadProgress` allows handling of progress events for downloads
|
||||
// browser only
|
||||
onDownloadProgress: function (progressEvent) {
|
||||
// Do whatever you want with the native progress event
|
||||
},
|
||||
|
||||
// `maxContentLength` defines the max size of the http response content in bytes allowed
|
||||
// `maxContentLength` defines the max size of the http response content in bytes allowed in node.js
|
||||
maxContentLength: 2000,
|
||||
|
||||
// `maxBodyLength` (Node only option) defines the max size of the http request content in bytes allowed
|
||||
maxBodyLength: 2000,
|
||||
|
||||
// `validateStatus` defines whether to resolve or reject the promise for a given
|
||||
// HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
|
||||
// or `undefined`), the promise will be resolved; otherwise, the promise will be
|
||||
@@ -377,7 +428,7 @@ These are the available config options for making requests. Only the `url` is re
|
||||
httpAgent: new http.Agent({ keepAlive: true }),
|
||||
httpsAgent: new https.Agent({ keepAlive: true }),
|
||||
|
||||
// 'proxy' defines the hostname and port of the proxy server.
|
||||
// `proxy` defines the hostname, port, and protocol of the proxy server.
|
||||
// You can also define your proxy using the conventional `http_proxy` and
|
||||
// `https_proxy` environment variables. If you are using environment variables
|
||||
// for your proxy configuration, you can also define a `no_proxy` environment
|
||||
@@ -387,7 +438,9 @@ These are the available config options for making requests. Only the `url` is re
|
||||
// supplies credentials.
|
||||
// This will set an `Proxy-Authorization` header, overwriting any existing
|
||||
// `Proxy-Authorization` custom headers you have set using `headers`.
|
||||
// If the proxy server uses HTTPS, then you must set the protocol to `https`.
|
||||
proxy: {
|
||||
protocol: 'https',
|
||||
host: '127.0.0.1',
|
||||
port: 9000,
|
||||
auth: {
|
||||
@@ -399,7 +452,38 @@ These are the available config options for making requests. Only the `url` is re
|
||||
// `cancelToken` specifies a cancel token that can be used to cancel the request
|
||||
// (see Cancellation section below for details)
|
||||
cancelToken: new CancelToken(function (cancel) {
|
||||
})
|
||||
}),
|
||||
|
||||
// an alternative way to cancel Axios requests using AbortController
|
||||
signal: new AbortController().signal,
|
||||
|
||||
// `decompress` indicates whether or not the response body should be decompressed
|
||||
// automatically. If set to `true` will also remove the 'content-encoding' header
|
||||
// from the responses objects of all decompressed responses
|
||||
// - Node only (XHR cannot turn off decompression)
|
||||
decompress: true // default
|
||||
|
||||
// `insecureHTTPParser` boolean.
|
||||
// Indicates where to use an insecure HTTP parser that accepts invalid HTTP headers.
|
||||
// This may allow interoperability with non-conformant HTTP implementations.
|
||||
// Using the insecure parser should be avoided.
|
||||
// see options https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_http_request_url_options_callback
|
||||
// see also https://nodejs.org/en/blog/vulnerability/february-2020-security-releases/#strict-http-header-parsing-none
|
||||
insecureHTTPParser: undefined // default
|
||||
|
||||
// transitional options for backward compatibility that may be removed in the newer versions
|
||||
transitional: {
|
||||
// silent JSON parsing mode
|
||||
// `true` - ignore JSON parsing errors and set response.data to null if parsing failed (old behaviour)
|
||||
// `false` - throw SyntaxError if JSON parsing failed (Note: responseType must be set to 'json')
|
||||
silentJSONParsing: true, // default value for the current Axios version
|
||||
|
||||
// try to parse the response string as JSON even if `responseType` is not 'json'
|
||||
forcedJSONParsing: true,
|
||||
|
||||
// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
|
||||
clarifyTimeoutError: false,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -418,8 +502,9 @@ The response for a request contains the following information.
|
||||
// `statusText` is the HTTP status message from the server response
|
||||
statusText: 'OK',
|
||||
|
||||
// `headers` the headers that the server responded with
|
||||
// All header names are lower cased
|
||||
// `headers` the HTTP headers that the server responded with
|
||||
// All header names are lower cased and can be accessed using the bracket notation.
|
||||
// Example: `response.headers['content-type']`
|
||||
headers: {},
|
||||
|
||||
// `config` is the config that was provided to `axios` for the request
|
||||
@@ -455,7 +540,11 @@ You can specify config defaults that will be applied to every request.
|
||||
|
||||
```js
|
||||
axios.defaults.baseURL = 'https://api.example.com';
|
||||
|
||||
// Important: If axios is used with multiple domains, the AUTH_TOKEN will be sent to all of them.
|
||||
// See below for an example using Custom instance defaults instead.
|
||||
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
|
||||
|
||||
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
|
||||
```
|
||||
|
||||
@@ -530,6 +619,49 @@ const instance = axios.create();
|
||||
instance.interceptors.request.use(function () {/*...*/});
|
||||
```
|
||||
|
||||
When you add request interceptors, they are presumed to be asynchronous by default. This can cause a delay
|
||||
in the execution of your axios request when the main thread is blocked (a promise is created under the hood for
|
||||
the interceptor and your request gets put on the bottom of the call stack). If your request interceptors are synchronous you can add a flag
|
||||
to the options object that will tell axios to run the code synchronously and avoid any delays in request execution.
|
||||
|
||||
```js
|
||||
axios.interceptors.request.use(function (config) {
|
||||
config.headers.test = 'I am only a header!';
|
||||
return config;
|
||||
}, null, { synchronous: true });
|
||||
```
|
||||
|
||||
If you want to execute a particular interceptor based on a runtime check,
|
||||
you can add a `runWhen` function to the options object. The interceptor will not be executed **if and only if** the return
|
||||
of `runWhen` is `false`. The function will be called with the config
|
||||
object (don't forget that you can bind your own arguments to it as well.) This can be handy when you have an
|
||||
asynchronous request interceptor that only needs to run at certain times.
|
||||
|
||||
```js
|
||||
function onGetCall(config) {
|
||||
return config.method === 'get';
|
||||
}
|
||||
axios.interceptors.request.use(function (config) {
|
||||
config.headers.test = 'special get headers';
|
||||
return config;
|
||||
}, null, { runWhen: onGetCall });
|
||||
```
|
||||
|
||||
### Multiple Interceptors
|
||||
|
||||
Given you add multiple response interceptors
|
||||
and when the response was fulfilled
|
||||
- then each interceptor is executed
|
||||
- then they are executed in the order they were added
|
||||
- then only the last interceptor's result is returned
|
||||
- then every interceptor receives the result of it's predecessor
|
||||
- and when the fulfillment-interceptor throws
|
||||
- then the following fulfillment-interceptor is not called
|
||||
- then the following rejection-interceptor is called
|
||||
- once caught, another following fulfill-interceptor is called again (just like in a promise chain).
|
||||
|
||||
Read [the interceptor tests](./test/specs/interceptors.spec.js) for seeing all this in code.
|
||||
|
||||
## Handling Errors
|
||||
|
||||
```js
|
||||
@@ -559,7 +691,7 @@ Using the `validateStatus` config option, you can define HTTP code(s) that shoul
|
||||
```js
|
||||
axios.get('/user/12345', {
|
||||
validateStatus: function (status) {
|
||||
return status < 500; // Reject only if the status code is greater than or equal to 500
|
||||
return status < 500; // Resolve only if the status code is less than 500
|
||||
}
|
||||
})
|
||||
```
|
||||
@@ -622,7 +754,21 @@ axios.get('/user/12345', {
|
||||
cancel();
|
||||
```
|
||||
|
||||
> Note: you can cancel several requests with the same cancel token.
|
||||
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.
|
||||
|
||||
## Using application/x-www-form-urlencoded format
|
||||
|
||||
@@ -664,6 +810,8 @@ axios(options);
|
||||
|
||||
### Node.js
|
||||
|
||||
#### Query string
|
||||
|
||||
In node.js, you can use the [`querystring`](https://nodejs.org/api/querystring.html) module as follows:
|
||||
|
||||
```js
|
||||
@@ -671,11 +819,45 @@ const querystring = require('querystring');
|
||||
axios.post('http://something.com/', querystring.stringify({ foo: 'bar' }));
|
||||
```
|
||||
|
||||
or ['URLSearchParams'](https://nodejs.org/api/url.html#url_class_urlsearchparams) from ['url module'](https://nodejs.org/api/url.html) as follows:
|
||||
|
||||
```js
|
||||
const url = require('url');
|
||||
const params = new url.URLSearchParams({ foo: 'bar' });
|
||||
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).
|
||||
|
||||
#### Form data
|
||||
|
||||
In node.js, you can use the [`form-data`](https://github.com/form-data/form-data) library as follows:
|
||||
|
||||
```js
|
||||
const FormData = require('form-data');
|
||||
|
||||
const form = new FormData();
|
||||
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, { 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;
|
||||
});
|
||||
```
|
||||
|
||||
## Semver
|
||||
|
||||
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.
|
||||
@@ -686,12 +868,30 @@ axios depends on a native ES6 Promise implementation to be [supported](http://ca
|
||||
If your environment doesn't support ES6 Promises, you can [polyfill](https://github.com/jakearchibald/es6-promise).
|
||||
|
||||
## TypeScript
|
||||
axios includes [TypeScript](http://typescriptlang.org) definitions.
|
||||
|
||||
axios includes [TypeScript](http://typescriptlang.org) definitions and a type guard for axios errors.
|
||||
|
||||
```typescript
|
||||
import axios from 'axios';
|
||||
axios.get('/user?ID=12345');
|
||||
let user: User = null;
|
||||
try {
|
||||
const { data } = await axios.get('/user?ID=12345');
|
||||
user = data.userDetails;
|
||||
} catch (error) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
handleAxiosError(error);
|
||||
} else {
|
||||
handleUnexpectedError(error);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Online one-click setup
|
||||
|
||||
You can use Gitpod an online IDE(which is free for Open Source) for contributing or running the examples online.
|
||||
|
||||
[](https://gitpod.io/#https://github.com/axios/axios/blob/master/examples/server.js)
|
||||
|
||||
|
||||
## Resources
|
||||
|
||||
* [Changelog](https://github.com/axios/axios/blob/master/CHANGELOG.md)
|
||||
@@ -702,7 +902,7 @@ axios.get('/user?ID=12345');
|
||||
|
||||
## Credits
|
||||
|
||||
axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/ng/service/$http) provided in [Angular](https://angularjs.org/). Ultimately axios is an effort to provide a standalone `$http`-like service for use outside of Angular.
|
||||
axios is heavily inspired by the [$http service](https://docs.angularjs.org/api/ng/service/$http) provided in [AngularJS](https://angularjs.org/). Ultimately axios is an effort to provide a standalone `$http`-like service for use outside of AngularJS.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
Reference in New Issue
Block a user