mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding option to disable automatic decompression (#2661)
* Adding ability to disable auto decompression * Updating decompress documentation in README * Fixing test\unit\adapters\http.js lint errors * Adding test for disabling auto decompression * Removing changes that fixed lint errors in tests * Removing formating change to unit test Co-authored-by: Xianming Zhong <chinesedfan@qq.com>
This commit is contained in:
committed by
GitHub
parent
6642ca9aa1
commit
42eb9dfabc
@@ -410,7 +410,14 @@ 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
|
// `cancelToken` specifies a cancel token that can be used to cancel the request
|
||||||
// (see Cancellation section below for details)
|
// (see Cancellation section below for details)
|
||||||
cancelToken: new CancelToken(function (cancel) {
|
cancelToken: new CancelToken(function (cancel) {
|
||||||
})
|
}),
|
||||||
|
|
||||||
|
// `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
|
||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Vendored
+1
@@ -69,6 +69,7 @@ export interface AxiosRequestConfig {
|
|||||||
httpsAgent?: any;
|
httpsAgent?: any;
|
||||||
proxy?: AxiosProxyConfig | false;
|
proxy?: AxiosProxyConfig | false;
|
||||||
cancelToken?: CancelToken;
|
cancelToken?: CancelToken;
|
||||||
|
decompress?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosResponse<T = any> {
|
export interface AxiosResponse<T = any> {
|
||||||
|
|||||||
+15
-10
@@ -181,20 +181,25 @@ module.exports = function httpAdapter(config) {
|
|||||||
|
|
||||||
// uncompress the response body transparently if required
|
// uncompress the response body transparently if required
|
||||||
var stream = res;
|
var stream = res;
|
||||||
|
|
||||||
// return the last request in case of redirects
|
// return the last request in case of redirects
|
||||||
var lastRequest = res.req || req;
|
var lastRequest = res.req || req;
|
||||||
|
|
||||||
switch (res.headers['content-encoding']) {
|
|
||||||
/*eslint default-case:0*/
|
|
||||||
case 'gzip':
|
|
||||||
case 'compress':
|
|
||||||
case 'deflate':
|
|
||||||
// add the unzipper to the body stream processing pipeline
|
|
||||||
stream = (res.statusCode === 204 || lastRequest.method === 'HEAD') ? stream : stream.pipe(zlib.createUnzip());
|
|
||||||
|
|
||||||
// remove the content-encoding in order to not confuse downstream operations
|
// if no content, is HEAD request or decompress disabled we should not decompress
|
||||||
delete res.headers['content-encoding'];
|
if (res.statusCode !== 204 && lastRequest.method !== 'HEAD' && config.decompress !== false) {
|
||||||
break;
|
switch (res.headers['content-encoding']) {
|
||||||
|
/*eslint default-case:0*/
|
||||||
|
case 'gzip':
|
||||||
|
case 'compress':
|
||||||
|
case 'deflate':
|
||||||
|
// add the unzipper to the body stream processing pipeline
|
||||||
|
stream = stream.pipe(zlib.createUnzip());
|
||||||
|
|
||||||
|
// remove the content-encoding in order to not confuse downstream operations
|
||||||
|
delete res.headers['content-encoding'];
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var response = {
|
var response = {
|
||||||
|
|||||||
@@ -192,6 +192,27 @@ describe('supports http with nodejs', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support disabling automatic decompression of response data', function(done) {
|
||||||
|
var data = 'Test data';
|
||||||
|
|
||||||
|
zlib.gzip(data, function(err, zipped) {
|
||||||
|
server = http.createServer(function(req, res) {
|
||||||
|
res.setHeader('Content-Type', 'text/html;charset=utf-8');
|
||||||
|
res.setHeader('Content-Encoding', 'gzip');
|
||||||
|
res.end(zipped);
|
||||||
|
}).listen(4444, function() {
|
||||||
|
axios.get('http://localhost:4444/', {
|
||||||
|
decompress: false,
|
||||||
|
responseType: 'arraybuffer'
|
||||||
|
|
||||||
|
}).then(function(res) {
|
||||||
|
assert.equal(res.data.toString('base64'), zipped.toString('base64'));
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should support UTF8', function (done) {
|
it('should support UTF8', function (done) {
|
||||||
var str = Array(100000).join('ж');
|
var str = Array(100000).join('ж');
|
||||||
|
|
||||||
@@ -731,4 +752,3 @@ describe('supports http with nodejs', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user