Skip to content

Commit 81f2537

Browse files
pietermeessindresorhus
authored andcommitted
Do not override Accept-Encoding header if already provided (#472)
1 parent 7d1aa01 commit 81f2537

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ function normalizeArguments(url, opts) {
527527
'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`
528528
}, headers);
529529

530-
if (opts.decompress) {
531-
opts.headers['accept-encoding'] = 'gzip,deflate';
530+
if (opts.decompress && is.undefined(opts.headers['accept-encoding'])) {
531+
opts.headers['accept-encoding'] = 'gzip, deflate';
532532
}
533533

534534
const query = opts.query;

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ request the resource pointed to in the location header via `GET`. This is in acc
195195
Type: `boolean`<br>
196196
Default: `true`
197197

198-
Decompress the response automatically.
198+
Decompress the response automatically. This will set the `accept-encoding` header to `gzip, deflate` unless you set it yourself.
199199

200200
If this is disabled, a compressed response is returned as a `Buffer`. This may be useful if you want to handle decompression yourself or stream the raw compressed data.
201201

test/headers.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ test('user-agent', async t => {
2424

2525
test('accept-encoding', async t => {
2626
const headers = (await got(s.url, {json: true})).body;
27-
t.is(headers['accept-encoding'], 'gzip,deflate');
27+
t.is(headers['accept-encoding'], 'gzip, deflate');
28+
});
29+
30+
test('do not override accept-encoding', async t => {
31+
const headers = (await got(s.url, {
32+
json: true,
33+
headers: {
34+
'accept-encoding': 'gzip'
35+
}
36+
})).body;
37+
t.is(headers['accept-encoding'], 'gzip');
2838
});
2939

3040
test('do not set accept-encoding header when decompress options is false', async t => {

0 commit comments

Comments
 (0)