Skip to content

Commit

Permalink
Make it possible to unset the default user-agent header (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak authored and sindresorhus committed Jul 5, 2018
1 parent f621184 commit e473a26
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
17 changes: 16 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>
Default: `{}`

Request headers.

Existing headers will be overwritten. Headers set to `null` or `undefined` will be omitted.

###### stream

Type: `boolean`<br>
Expand Down Expand Up @@ -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');
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions test/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit e473a26

Please # to comment.