Repository: sindresorhus/got (ts)

Issue:
Don't include a given request header when its value is null or undefined
If you do `got(url, { headers: { cookie: null } })`, the HTTP request has a header of `cookie: null` rather than not appearing at all. If you replace `null` with `undefined` then you get an error.

If a header is `null` or `undefined` the it shouldn't be added to the request headers.

Pull request:
Exclude passed headers with values `null` or `undefined`
**Notes:**
- Extract call to `lowercaseKeys` to leverage the object-safety it provides
- Delete headers with a value of `null` or `undefined`
- Considered exclusion of `''` as well, but this seems more common / legitimate from my research

Closes #410 


Patch:
diff --git a/index.js b/index.js
index 09ea52a..98123fb 100644
--- a/index.js
+++ b/index.js
@@ -458,10 +458,17 @@ function normalizeArguments(url, opts) {
 		opts
 	);
 
+	const headers = lowercaseKeys(opts.headers);
+	for (const key of Object.keys(headers)) {
+		if (headers[key] === null || headers[key] === undefined) {
+			delete headers[key];
+		}
+	}
+
 	opts.headers = Object.assign({
 		'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`,
 		'accept-encoding': 'gzip,deflate'
-	}, lowercaseKeys(opts.headers));
+	}, headers);
 
 	const query = opts.query;
 


Test patch:
diff --git a/test/headers.js b/test/headers.js
index 2a0a0ae..07fa774 100644
--- a/test/headers.js
+++ b/test/headers.js
@@ -89,6 +89,24 @@ test('form-data automatic content-type', async t => {
 	t.is(headers['content-type'], `multipart/form-data; boundary=${form.getBoundary()}`);
 });
 
+test('remove null value headers', async t => {
+	const headers = (await got(s.url, {
+		headers: {
+			unicorns: null
+		}
+	})).body;
+	t.false(Object.prototype.hasOwnProperty.call(headers, 'unicorns'));
+});
+
+test('remove undefined value headers', async t => {
+	const headers = (await got(s.url, {
+		headers: {
+			unicorns: undefined
+		}
+	})).body;
+	t.false(Object.prototype.hasOwnProperty.call(headers, 'unicorns'));
+});
+
 test.after('cleanup', async () => {
 	await s.close();
 });