From e473a26de25f8cc8fa34f5285f52aa01bb6065eb Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Thu, 5 Jul 2018 17:00:20 +0200 Subject: [PATCH] Make it possible to unset the default `user-agent` header (#496) --- index.js | 4 ++++ readme.md | 17 ++++++++++++++++- test/headers.js | 8 ++++---- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 4c8f417ee..074fdeb63 100644 --- a/index.js +++ b/index.js @@ -546,6 +546,10 @@ function normalizeArguments(url, opts) { }; const headers = lowercaseKeys(opts.headers); + if (!Object.keys(headers).includes('user-agent')) { + headers['user-agent'] = `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`; + } + for (const [key, value] of Object.entries(headers)) { if (is.nullOrUndefined(value)) { delete headers[key]; diff --git a/readme.md b/readme.md index 5401674bb..c190f8607 100644 --- a/readme.md +++ b/readme.md @@ -105,6 +105,15 @@ Type: `Object` Any of the [`https.request`](https://nodejs.org/api/https.html#https_https_request_options_callback) options. +###### headers + +Type: `Object`
+Default: `{}` + +Request headers. + +Existing headers will be overwritten. Headers set to `null` or `undefined` will be omitted. + ###### stream Type: `boolean`
@@ -635,7 +644,7 @@ const createTestServer = require('create-test-server'); ### User Agent -It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo. +It's a good idea to set the `'user-agent'` header so the provider can more easily see how their resource is used. By default, it's the URL to this repo. You can omit this header by setting it to `null` or `undefined`. ```js const got = require('got'); @@ -646,6 +655,12 @@ got('sindresorhus.com', { 'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)` } }); + +got('sindresorhus.com', { + headers: { + 'user-agent': null + } +}); ``` ### 304 Responses diff --git a/test/headers.js b/test/headers.js index 82d528e28..f7d6537b8 100644 --- a/test/headers.js +++ b/test/headers.js @@ -128,21 +128,21 @@ test('stream as options.body sets content-length', async t => { test('remove null value headers', async t => { const {body} = await got(s.url, { headers: { - unicorns: null + 'user-agent': null } }); const headers = JSON.parse(body); - t.false(Reflect.has(headers, 'unicorns')); + t.false(Reflect.has(headers, 'user-agent')); }); test('remove undefined value headers', async t => { const {body} = await got(s.url, { headers: { - unicorns: undefined + 'user-agent': undefined } }); const headers = JSON.parse(body); - t.false(Reflect.has(headers, 'unicorns')); + t.false(Reflect.has(headers, 'user-agent')); }); test.after('cleanup', async () => {