Skip to content

Commit c5e2f8d

Browse files
brandon93ssindresorhus
authored andcommitted
Exclude passed headers with values null or undefined (#413)
1 parent 0c5e44c commit c5e2f8d

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,17 @@ function normalizeArguments(url, opts) {
458458
opts
459459
);
460460

461+
const headers = lowercaseKeys(opts.headers);
462+
for (const key of Object.keys(headers)) {
463+
if (headers[key] === null || headers[key] === undefined) {
464+
delete headers[key];
465+
}
466+
}
467+
461468
opts.headers = Object.assign({
462469
'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`,
463470
'accept-encoding': 'gzip,deflate'
464-
}, lowercaseKeys(opts.headers));
471+
}, headers);
465472

466473
const query = opts.query;
467474

test/headers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ test('form-data automatic content-type', async t => {
8989
t.is(headers['content-type'], `multipart/form-data; boundary=${form.getBoundary()}`);
9090
});
9191

92+
test('remove null value headers', async t => {
93+
const headers = (await got(s.url, {
94+
headers: {
95+
unicorns: null
96+
}
97+
})).body;
98+
t.false(Object.prototype.hasOwnProperty.call(headers, 'unicorns'));
99+
});
100+
101+
test('remove undefined value headers', async t => {
102+
const headers = (await got(s.url, {
103+
headers: {
104+
unicorns: undefined
105+
}
106+
})).body;
107+
t.false(Object.prototype.hasOwnProperty.call(headers, 'unicorns'));
108+
});
109+
92110
test.after('cleanup', async () => {
93111
await s.close();
94112
});

0 commit comments

Comments
 (0)