UNPKG

37.8 kBMarkdownView Raw
1<div align="center">
2 <br>
3 <br>
4 <img width="360" src="media/logo.svg" alt="Got">
5 <br>
6 <br>
7 <br>
8 <p align="center">Huge thanks to <a href="https://moxy.studio"><img src="https://sindresorhus.com/assets/thanks/moxy-logo.svg" width="150"></a> for sponsoring me!
9 </p>
10 <br>
11 <br>
12</div>
13
14> Simplified HTTP requests
15
16[![Build Status: Linux](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/got/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master) [![Downloads](https://img.shields.io/npm/dm/got.svg)](https://npmjs.com/got) [![Install size](https://packagephobia.now.sh/badge?p=got)](https://packagephobia.now.sh/result?p=got)
17
18Got is a human-friendly and powerful HTTP request library.
19
20It was created because the popular [`request`](https://github.com/request/request) package is bloated: [![Install size](https://packagephobia.now.sh/badge?p=request)](https://packagephobia.now.sh/result?p=request)
21
22Got is for Node.js. For browsers, we recommend [Ky](https://github.com/sindresorhus/ky).
23
24
25## Highlights
26
27- [Promise & stream API](#api)
28- [Request cancelation](#aborting-the-request)
29- [RFC compliant caching](#cache-adapters)
30- [Follows redirects](#followredirect)
31- [Retries on failure](#retry)
32- [Progress events](#onuploadprogress-progress)
33- [Handles gzip/deflate](#decompress)
34- [Timeout handling](#timeout)
35- [Errors with metadata](#errors)
36- [JSON mode](#json)
37- [WHATWG URL support](#url)
38- [Hooks](https://github.com/sindresorhus/got#hooks)
39- [Instances with custom defaults](#instances)
40- [Composable](advanced-creation.md#merging-instances)
41- [Electron support](#useelectronnet)
42- [Used by ~2000 packages and ~500K repos](https://github.com/sindresorhus/got/network/dependents)
43- Actively maintained
44
45[See how Got compares to other HTTP libraries](#comparison)
46
47
48## Install
49
50```
51$ npm install got
52```
53
54<a href="https://www.patreon.com/sindresorhus">
55 <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
56</a>
57
58
59## Usage
60
61```js
62const got = require('got');
63
64(async () => {
65 try {
66 const response = await got('sindresorhus.com');
67 console.log(response.body);
68 //=> '<!doctype html> ...'
69 } catch (error) {
70 console.log(error.response.body);
71 //=> 'Internal server error ...'
72 }
73})();
74```
75
76###### Streams
77
78```js
79const fs = require('fs');
80const got = require('got');
81
82got.stream('sindresorhus.com').pipe(fs.createWriteStream('index.html'));
83
84// For POST, PUT, and PATCH methods `got.stream` returns a `stream.Writable`
85fs.createReadStream('index.html').pipe(got.stream.post('sindresorhus.com'));
86```
87
88
89### API
90
91It's a `GET` request by default, but can be changed by using different methods or in the `options`.
92
93#### got(url, [options])
94
95Returns a Promise for a [`response` object](#response) or a [stream](#streams-1) if `options.stream` is set to true.
96
97##### url
98
99Type: `string` `Object`
100
101The URL to request, as a string, a [`https.request` options object](https://nodejs.org/api/https.html#https_https_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
102
103Properties from `options` will override properties in the parsed `url`.
104
105If no protocol is specified, it will default to `https`.
106
107##### options
108
109Type: `Object`
110
111Any of the [`https.request`](https://nodejs.org/api/https.html#https_https_request_options_callback) options.
112
113###### baseUrl
114
115Type: `string` `Object`
116
117When specified, `url` will be prepended by `baseUrl`.<br>
118If you specify an absolute URL, it will skip the `baseUrl`.
119
120Very useful when used with `got.extend()` to create niche-specific Got instances.
121
122Can be a string or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
123
124Slash at the end of `baseUrl` and at the beginning of the `url` argument is optional:
125
126```js
127await got('hello', {baseUrl: 'https://example.com/v1'});
128//=> 'https://example.com/v1/hello'
129
130await got('/hello', {baseUrl: 'https://example.com/v1/'});
131//=> 'https://example.com/v1/hello'
132
133await got('/hello', {baseUrl: 'https://example.com/v1'});
134//=> 'https://example.com/v1/hello'
135```
136
137###### headers
138
139Type: `Object`<br>
140Default: `{}`
141
142Request headers.
143
144Existing headers will be overwritten. Headers set to `null` will be omitted.
145
146###### stream
147
148Type: `boolean`<br>
149Default: `false`
150
151Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.stream(url, [options])`.
152
153###### body
154
155Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data)
156
157*If you provide this option, `got.stream()` will be read-only.*
158
159The body that will be sent with a `POST` request.
160
161If present in `options` and `options.method` is not set, `options.method` will be set to `POST`.
162
163The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
164
165###### cookieJar
166
167Type: [`tough.CookieJar` instance](https://github.com/salesforce/tough-cookie#cookiejar)
168
169Cookie support. You don't have to care about parsing or how to store them. [Example.](#cookies)
170
171**Note:** `options.headers.cookie` will be overridden.
172
173###### encoding
174
175Type: `string` `null`<br>
176Default: `'utf8'`
177
178[Encoding](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) to be used on `setEncoding` of the response data. If `null`, the body is returned as a [`Buffer`](https://nodejs.org/api/buffer.html) (binary data).
179
180###### form
181
182Type: `boolean`<br>
183Default: `false`
184
185*If you provide this option, `got.stream()` will be read-only.*
186
187If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`.
188
189`body` must be a plain object. It will be converted to a query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj).
190
191###### json
192
193Type: `boolean`<br>
194Default: `false`
195
196*If you use `got.stream()`, this option will be ignored.*
197
198If set to `true` and `Content-Type` header is not set, it will be set to `application/json`.
199
200Parse response body with `JSON.parse` and set `accept` header to `application/json`. If used in conjunction with the `form` option, the `body` will the stringified as querystring and the response parsed as JSON.
201
202`body` must be a plain object or array and will be stringified.
203
204###### query
205
206Type: `string` `Object<string, string|number>` [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
207
208Query string that will be added to the request URL. This will override the query string in `url`.
209
210If you need to pass in an array, you can do it using a `URLSearchParams` instance:
211
212```js
213const got = require('got');
214
215const query = new URLSearchParams([['key', 'a'], ['key', 'b']]);
216
217got('https://example.com', {query});
218
219console.log(query.toString());
220//=> 'key=a&key=b'
221```
222
223And if you need a different array format, you could use the [`query-string`](https://github.com/sindresorhus/query-string) package:
224
225```js
226const got = require('got');
227const queryString = require('query-string');
228
229const query = queryString.stringify({key: ['a', 'b']}, {arrayFormat: 'bracket'});
230
231got('https://example.com', {query});
232
233console.log(query);
234//=> 'key[]=a&key[]=b'
235```
236
237###### timeout
238
239Type: `number` `Object`
240
241Milliseconds to wait for the server to end the response before aborting the request with [`got.TimeoutError`](#gottimeouterror) error (a.k.a. `request` property). By default, there's no timeout.
242
243This also accepts an `object` with the following fields to constrain the duration of each phase of the request lifecycle:
244
245- `lookup` starts when a socket is assigned and ends when the hostname has been resolved. Does not apply when using a Unix domain socket.
246- `connect` starts when `lookup` completes (or when the socket is assigned if lookup does not apply to the request) and ends when the socket is connected.
247- `secureConnect` starts when `connect` completes and ends when the handshaking process completes (HTTPS only).
248- `socket` starts when the socket is connected. See [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback).
249- `response` starts when the request has been written to the socket and ends when the response headers are received.
250- `send` starts when the socket is connected and ends with the request has been written to the socket.
251- `request` starts when the request is initiated and ends when the response's end event fires.
252
253###### retry
254
255Type: `number` `Object`<br>
256Default:
257- retries: `2`
258- methods: `GET` `PUT` `HEAD` `DELETE` `OPTIONS` `TRACE`
259- statusCodes: [`408`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) [`413`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413) [`429`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) [`500`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) [`502`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502) [`503`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503) [`504`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/504)
260- maxRetryAfter: `undefined`
261
262An object representing `retries`, `methods`, `statusCodes` and `maxRetryAfter` fields for the time until retry, allowed methods, allowed status codes and maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time.
263
264If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.<br>
265If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
266
267Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 0).
268
269The `retries` property can be a `number` or a `function` with `retry` and `error` arguments. The function must return a delay in milliseconds (`0` return value cancels retry).
270
271**Note:** It retries only on the specified methods, status codes, and on these network errors:
272- `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
273- `ECONNRESET`: Connection was forcibly closed by a peer.
274- `EADDRINUSE`: Could not bind to any free port.
275- `ECONNREFUSED`: Connection was refused by the server.
276- `EPIPE`: The remote side of the stream being written has been closed.
277
278###### followRedirect
279
280Type: `boolean`<br>
281Default: `true`
282
283Defines if redirect responses should be followed automatically.
284
285Note that if a `303` is sent by the server in response to any request type (`POST`, `DELETE`, etc.), Got will automatically request the resource pointed to in the location header via `GET`. This is in accordance with [the spec](https://tools.ietf.org/html/rfc7231#section-6.4.4).
286
287###### decompress
288
289Type: `boolean`<br>
290Default: `true`
291
292Decompress the response automatically. This will set the `accept-encoding` header to `gzip, deflate` unless you set it yourself.
293
294If 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.
295
296###### cache
297
298Type: `Object`<br>
299Default: `false`
300
301[Cache adapter instance](#cache-adapters) for storing cached data.
302
303###### request
304
305Type: `Function`<br>
306Default: `http.request` `https.request` *(depending on the protocol)*
307
308Custom request function. The main purpose of this is to [support HTTP2 using a wrapper](#experimental-http2-support).
309
310###### useElectronNet
311
312Type: `boolean`<br>
313Default: `false`
314
315When used in Electron, Got will use [`electron.net`](https://electronjs.org/docs/api/net/) instead of the Node.js `http` module. According to the Electron docs, it should be fully compatible, but it's not entirely. See [#443](https://github.com/sindresorhus/got/issues/443) and [#461](https://github.com/sindresorhus/got/issues/461).
316
317###### throwHttpErrors
318
319Type: `boolean`<br>
320Default: `true`
321
322Determines if a `got.HTTPError` is thrown for error responses (non-2xx status codes).
323
324If this is disabled, requests that encounter an error status code will be resolved with the `response` instead of throwing. This may be useful if you are checking for resource availability and are expecting error responses.
325
326###### agent
327
328Same as the [`agent` option](https://nodejs.org/api/http.html#http_http_request_url_options_callback) for `http.request`, but with an extra feature:
329
330If you require different agents for different protocols, you can pass a map of agents to the `agent` option. This is necessary because a request to one protocol might redirect to another. In such a scenario, Got will switch over to the right protocol agent for you.
331
332```js
333const got = require('got');
334const HttpAgent = require('agentkeepalive');
335const {HttpsAgent} = HttpAgent;
336
337got('sindresorhus.com', {
338 agent: {
339 http: new HttpAgent(),
340 https: new HttpsAgent()
341 }
342});
343```
344
345###### hooks
346
347Type: `Object<string, Function[]>`
348
349Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially.
350
351###### hooks.beforeRequest
352
353Type: `Function[]`<br>
354Default: `[]`
355
356Called with [normalized](source/normalize-arguments.js) [request options](#options). Got will make no further changes to the request before it is sent. This is especially useful in conjunction with [`got.extend()`](#instances) and [`got.create()`](advanced-creation.md) when you want to create an API client that, for example, uses HMAC-signing.
357
358See the [AWS section](#aws) for an example.
359
360**Note**: If you modify the `body` you will need to modify the `content-length` header too, because it has already been computed and assigned.
361
362###### hooks.beforeRedirect
363
364Type: `Function[]`<br>
365Default: `[]`
366
367Called with [normalized](source/normalize-arguments.js) [request options](#options). Got will make no further changes to the request. This is especially useful when you want to avoid dead sites. Example:
368
369```js
370const got = require('got');
371
372got('example.com', {
373 hooks: {
374 beforeRedirect: [
375 options => {
376 if (options.hostname === 'deadSite') {
377 options.hostname = 'fallbackSite';
378 }
379 }
380 ]
381 }
382});
383```
384
385###### hooks.beforeRetry
386
387Type: `Function[]`<br>
388Default: `[]`
389
390Called with [normalized](source/normalize-arguments.js) [request options](#options), the error and the retry count. Got will make no further changes to the request. This is especially useful when some extra work is required before the next try. Example:
391
392```js
393const got = require('got');
394
395got('example.com', {
396 hooks: {
397 beforeRetry: [
398 (options, error, retryCount) => {
399 if (error.statusCode === 413) { // Payload too large
400 options.body = getNewBody();
401 }
402 }
403 ]
404 }
405});
406```
407
408###### hooks.afterResponse
409
410Type: `Function[]`<br>
411Default: `[]`
412
413Called with [response object](#response) and a retry function.
414
415Each function should return the response. This is especially useful when you want to refresh an access token. Example:
416
417```js
418const got = require('got');
419
420const instance = got.extend({
421 hooks: {
422 afterResponse: [
423 (response, retryWithMergedOptions) => {
424 if (response.statusCode === 401) { // Unauthorized
425 const updatedOptions = {
426 headers: {
427 token: getNewToken() // Refresh the access token
428 }
429 };
430
431 // Save for further requests
432 instance.defaults.options = got.mergeOptions(instance.defaults.options, updatedOptions);
433
434 // Make a new retry
435 return retryWithMergedOptions(updatedOptions);
436 }
437
438 // No changes otherwise
439 return response;
440 }
441 ]
442 },
443 mutableDefaults: true
444});
445```
446
447#### Response
448
449The response object will typically be a [Node.js HTTP response stream](https://nodejs.org/api/http.html#http_class_http_incomingmessage), however, if returned from the cache it will be a [response-like object](https://github.com/lukechilds/responselike) which behaves in the same way.
450
451##### body
452
453Type: `string` `Object` *(depending on `options.json`)*
454
455The result of the request.
456
457##### url
458
459Type: `string`
460
461The request URL or the final URL after redirects.
462
463##### requestUrl
464
465Type: `string`
466
467The original request URL.
468
469##### timings
470
471Type: `Object`
472
473The object contains the following properties:
474
475- `start` - Time when the request started.
476- `socket` - Time when a socket was assigned to the request.
477- `lookup` - Time when the DNS lookup finished.
478- `connect` - Time when the socket successfully connected.
479- `upload` - Time when the request finished uploading.
480- `response` - Time when the request fired the `response` event.
481- `end` - Time when the response fired the `end` event.
482- `error` - Time when the request fired the `error` event.
483- `phases`
484 - `wait` - `timings.socket - timings.start`
485 - `dns` - `timings.lookup - timings.socket`
486 - `tcp` - `timings.connect - timings.lookup`
487 - `request` - `timings.upload - timings.connect`
488 - `firstByte` - `timings.response - timings.upload`
489 - `download` - `timings.end - timings.response`
490 - `total` - `timings.end - timings.start` or `timings.error - timings.start`
491
492**Note**: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
493
494##### fromCache
495
496Type: `boolean`
497
498Whether the response was retrieved from the cache.
499
500##### redirectUrls
501
502Type: `Array`
503
504The redirect URLs.
505
506##### retryCount
507
508Type: `number`
509
510The number of times the request was retried.
511
512#### Streams
513
514**Note**: Progress events, redirect events and request/response events can also be used with promises.
515
516#### got.stream(url, [options])
517
518Sets `options.stream` to `true`.
519
520Returns a [duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with additional events:
521
522##### .on('request', request)
523
524`request` event to get the request object of the request.
525
526**Tip**: You can use `request` event to abort request:
527
528```js
529got.stream('github.com')
530 .on('request', request => setTimeout(() => request.abort(), 50));
531```
532
533##### .on('response', response)
534
535The `response` event to get the response object of the final request.
536
537##### .on('redirect', response, nextOptions)
538
539The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
540
541##### .on('uploadProgress', progress)
542##### .on('downloadProgress', progress)
543
544Progress events for uploading (sending a request) and downloading (receiving a response). The `progress` argument is an object like:
545
546```js
547{
548 percent: 0.1,
549 transferred: 1024,
550 total: 10240
551}
552```
553
554If it's not possible to retrieve the body size (can happen when streaming), `total` will be `null`.
555
556```js
557(async () => {
558 const response = await got('sindresorhus.com')
559 .on('downloadProgress', progress => {
560 // Report download progress
561 })
562 .on('uploadProgress', progress => {
563 // Report upload progress
564 });
565
566 console.log(response);
567})();
568```
569
570##### .on('error', error, body, response)
571
572The `error` event emitted in case of a protocol error (like `ENOTFOUND` etc.) or status error (4xx or 5xx). The second argument is the body of the server response in case of status error. The third argument is a response object.
573
574#### got.get(url, [options])
575#### got.post(url, [options])
576#### got.put(url, [options])
577#### got.patch(url, [options])
578#### got.head(url, [options])
579#### got.delete(url, [options])
580
581Sets `options.method` to the method name and makes a request.
582
583### Instances
584
585#### got.extend([options])
586
587Configure a new `got` instance with default `options`. `options` are merged with the parent instance's `defaults.options` using [`got.mergeOptions`](#gotmergeoptionsparentoptions-newoptions).
588
589```js
590const client = got.extend({
591 baseUrl: 'https://example.com',
592 headers: {
593 'x-unicorn': 'rainbow'
594 }
595});
596
597client.get('/demo');
598
599/* HTTP Request =>
600 * GET /demo HTTP/1.1
601 * Host: example.com
602 * x-unicorn: rainbow
603 */
604```
605
606```js
607(async () => {
608 const client = got.extend({
609 baseUrl: 'httpbin.org',
610 headers: {
611 'x-foo': 'bar'
612 }
613 });
614 const {headers} = (await client.get('/headers', {json: true})).body;
615 //=> headers['x-foo'] === 'bar'
616
617 const jsonClient = client.extend({
618 json: true,
619 headers: {
620 'x-baz': 'qux'
621 }
622 });
623 const {headers: headers2} = (await jsonClient.get('/headers')).body;
624 //=> headers2['x-foo'] === 'bar'
625 //=> headers2['x-baz'] === 'qux'
626})();
627```
628
629*Need more control over the behavior of Got? Check out the [`got.create()`](advanced-creation.md).*
630
631#### got.mergeOptions(parentOptions, newOptions)
632
633Extends parent options. Avoid using [object spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals) as it doesn't work recursively:
634
635```js
636const a = {headers: {cat: 'meow', wolf: ['bark', 'wrrr']}};
637const b = {headers: {cow: 'moo', wolf: ['auuu']}};
638
639{...a, ...b} // => {headers: {cow: 'moo', wolf: ['auuu']}}
640got.mergeOptions(a, b) // => {headers: {cat: 'meow', cow: 'moo', wolf: ['auuu']}}
641```
642
643Options are deeply merged to a new object. The value of each key is determined as follows:
644
645- If the new property is set to `undefined`, it keeps the old one.
646- If the parent property is an instance of `URL` and the new value is a `string` or `URL`, a new URL instance is created: [`new URL(new, parent)`](https://developer.mozilla.org/en-US/docs/Web/API/URL/URL#Syntax).
647- If the new property is a plain `Object`:
648 - If the parent property is a plain `Object` too, both values are merged recursively into a new `Object`.
649 - Otherwise, only the new value is deeply cloned.
650- If the new property is an `Array`, it overwrites the old one with a deep clone of the new property.
651- Otherwise, the new value is assigned to the key.
652
653## Errors
654
655Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol` and `url` properties to make debugging easier.
656
657In Promise mode, the `response` is attached to the error.
658
659#### got.CacheError
660
661When a cache method fails, for example, if the database goes down or there's a filesystem error.
662
663#### got.RequestError
664
665When a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`.
666
667#### got.ReadError
668
669When reading from response stream fails.
670
671#### got.ParseError
672
673When `json` option is enabled, server response code is 2xx, and `JSON.parse` fails.
674
675#### got.HTTPError
676
677When the server response code is not 2xx. Includes `statusCode`, `statusMessage`, and `redirectUrls` properties.
678
679#### got.MaxRedirectsError
680
681When the server redirects you more than ten times. Includes a `redirectUrls` property, which is an array of the URLs Got was redirected to before giving up.
682
683#### got.UnsupportedProtocolError
684
685When given an unsupported protocol.
686
687#### got.CancelError
688
689When the request is aborted with `.cancel()`.
690
691#### got.TimeoutError
692
693When the request is aborted due to a [timeout](#timeout)
694
695## Aborting the request
696
697The promise returned by Got has a [`.cancel()`](https://github.com/sindresorhus/p-cancelable) method which when called, aborts the request.
698
699```js
700(async () => {
701 const request = got(url, options);
702
703 // …
704
705 // In another part of the code
706 if (something) {
707 request.cancel();
708 }
709
710 // …
711
712 try {
713 await request;
714 } catch (error) {
715 if (request.isCanceled) { // Or `error instanceof got.CancelError`
716 // Handle cancelation
717 }
718
719 // Handle other errors
720 }
721})();
722```
723
724<a name="cache-adapters"></a>
725## Cache
726
727Got implements [RFC 7234](http://httpwg.org/specs/rfc7234.html) compliant HTTP caching which works out of the box in-memory and is easily pluggable with a wide range of storage adapters. Fresh cache entries are served directly from the cache, and stale cache entries are revalidated with `If-None-Match`/`If-Modified-Since` headers. You can read more about the underlying cache behavior in the [`cacheable-request` documentation](https://github.com/lukechilds/cacheable-request).
728
729You can use the JavaScript `Map` type as an in-memory cache:
730
731```js
732const got = require('got');
733const map = new Map();
734
735(async () => {
736 let response = await got('sindresorhus.com', {cache: map});
737 console.log(response.fromCache);
738 //=> false
739
740 response = await got('sindresorhus.com', {cache: map});
741 console.log(response.fromCache);
742 //=> true
743})();
744```
745
746Got uses [Keyv](https://github.com/lukechilds/keyv) internally to support a wide range of storage adapters. For something more scalable you could use an [official Keyv storage adapter](https://github.com/lukechilds/keyv#official-storage-adapters):
747
748```
749$ npm install @keyv/redis
750```
751
752```js
753const got = require('got');
754const KeyvRedis = require('@keyv/redis');
755
756const redis = new KeyvRedis('redis://user:pass@localhost:6379');
757
758got('sindresorhus.com', {cache: redis});
759```
760
761Got supports anything that follows the Map API, so it's easy to write your own storage adapter or use a third-party solution.
762
763For example, the following are all valid storage adapters:
764
765```js
766const storageAdapter = new Map();
767// Or
768const storageAdapter = require('./my-storage-adapter');
769// Or
770const QuickLRU = require('quick-lru');
771const storageAdapter = new QuickLRU({maxSize: 1000});
772
773got('sindresorhus.com', {cache: storageAdapter});
774```
775
776View the [Keyv docs](https://github.com/lukechilds/keyv) for more information on how to use storage adapters.
777
778
779## Proxies
780
781You can use the [`tunnel`](https://github.com/koichik/node-tunnel) package with the `agent` option to work with proxies:
782
783```js
784const got = require('got');
785const tunnel = require('tunnel');
786
787got('sindresorhus.com', {
788 agent: tunnel.httpOverHttp({
789 proxy: {
790 host: 'localhost'
791 }
792 })
793});
794```
795
796Check out [`global-tunnel`](https://github.com/np-maintain/global-tunnel) if you want to configure proxy support for all HTTP/HTTPS traffic in your app.
797
798
799## Cookies
800
801You can use the [`tough-cookie`](https://github.com/salesforce/tough-cookie) package:
802
803```js
804const got = require('got');
805const {CookieJar} = require('tough-cookie');
806
807const cookieJar = new CookieJar();
808cookieJar.setCookie('foo=bar', 'https://www.google.com');
809
810got('google.com', {cookieJar});
811```
812
813
814## Form data
815
816You can use the [`form-data`](https://github.com/form-data/form-data) package to create POST request with form data:
817
818```js
819const fs = require('fs');
820const got = require('got');
821const FormData = require('form-data');
822const form = new FormData();
823
824form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
825
826got.post('google.com', {
827 body: form
828});
829```
830
831
832## OAuth
833
834You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) package to create a signed OAuth request:
835
836```js
837const got = require('got');
838const crypto = require('crypto');
839const OAuth = require('oauth-1.0a');
840
841const oauth = OAuth({
842 consumer: {
843 key: process.env.CONSUMER_KEY,
844 secret: process.env.CONSUMER_SECRET
845 },
846 signature_method: 'HMAC-SHA1',
847 hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
848});
849
850const token = {
851 key: process.env.ACCESS_TOKEN,
852 secret: process.env.ACCESS_TOKEN_SECRET
853};
854
855const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
856
857got(url, {
858 headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
859 json: true
860});
861```
862
863
864## Unix Domain Sockets
865
866Requests can also be sent via [unix domain sockets](http://serverfault.com/questions/124517/whats-the-difference-between-unix-socket-and-tcp-ip-socket). Use the following URL scheme: `PROTOCOL://unix:SOCKET:PATH`.
867
868- `PROTOCOL` - `http` or `https` *(optional)*
869- `SOCKET` - Absolute path to a unix domain socket, for example: `/var/run/docker.sock`
870- `PATH` - Request path, for example: `/v2/keys`
871
872```js
873got('http://unix:/var/run/docker.sock:/containers/json');
874
875// Or without protocol (HTTP by default)
876got('unix:/var/run/docker.sock:/containers/json');
877```
878
879
880## AWS
881
882Requests to AWS services need to have their headers signed. This can be accomplished by using the [`aws4`](https://www.npmjs.com/package/aws4) package. This is an example for querying an ["API Gateway"](https://docs.aws.amazon.com/apigateway/api-reference/signing-requests/) with a signed request.
883
884```js
885const AWS = require('aws-sdk');
886const aws4 = require('aws4');
887const got = require('got');
888
889const chain = new AWS.CredentialProviderChain();
890
891// Create a Got instance to use relative paths and signed requests
892const awsClient = got.extend({
893 baseUrl: 'https://<api-id>.execute-api.<api-region>.amazonaws.com/<stage>/',
894 hooks: {
895 beforeRequest: [
896 async options => {
897 const credentials = await chain.resolvePromise();
898 aws4.sign(options, credentials);
899 }
900 ]
901 }
902});
903
904const response = await awsClient('endpoint/path', {
905 // Request-specific options
906});
907```
908
909
910## Testing
911
912You can test your requests by using the [`nock`](https://github.com/node-nock/nock) package to mock an endpoint:
913
914```js
915const got = require('got');
916const nock = require('nock');
917
918nock('https://sindresorhus.com')
919 .get('/')
920 .reply(200, 'Hello world!');
921
922(async () => {
923 const response = await got('sindresorhus.com');
924 console.log(response.body);
925 //=> 'Hello world!'
926})();
927```
928
929If you need real integration tests you can use [`create-test-server`](https://github.com/lukechilds/create-test-server):
930
931```js
932const got = require('got');
933const createTestServer = require('create-test-server');
934
935(async () => {
936 const server = await createTestServer();
937 server.get('/', 'Hello world!');
938
939 const response = await got(server.url);
940 console.log(response.body);
941 //=> 'Hello world!'
942
943 await server.close();
944})();
945```
946
947
948## Tips
949
950### User Agent
951
952It'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`.
953
954```js
955const got = require('got');
956const pkg = require('./package.json');
957
958got('sindresorhus.com', {
959 headers: {
960 'user-agent': `my-package/${pkg.version} (https://github.com/username/my-package)`
961 }
962});
963
964got('sindresorhus.com', {
965 headers: {
966 'user-agent': null
967 }
968});
969```
970
971### 304 Responses
972
973Bear in mind; if you send an `if-modified-since` header and receive a `304 Not Modified` response, the body will be empty. It's your responsibility to cache and retrieve the body contents.
974
975### Custom endpoints
976
977Use `got.extend()` to make it nicer to work with REST APIs. Especially if you use the `baseUrl` option.
978
979**Note:** Not to be confused with [`got.create()`](advanced-creation.md), which has no defaults.
980
981```js
982const got = require('got');
983const pkg = require('./package.json');
984
985const custom = got.extend({
986 baseUrl: 'example.com',
987 json: true,
988 headers: {
989 'user-agent': `my-package/${pkg.version} (https://github.com/username/my-package)`
990 }
991});
992
993// Use `custom` exactly how you use `got`
994(async () => {
995 const list = await custom('/v1/users/list');
996})();
997```
998
999*Need to merge some instances into a single one? Check out [`got.mergeInstances()`](advanced-creation.md#merging-instances).*
1000
1001### Experimental HTTP2 support
1002
1003Got provides an experimental support for HTTP2 using the [`http2-wrapper`](https://github.com/szmarczak/http2-wrapper) package:
1004
1005```js
1006const got = require('got');
1007const {request} = require('http2-wrapper');
1008
1009const h2got = got.extend({request});
1010
1011(async () => {
1012 const {body} = await h2got('https://nghttp2.org/httpbin/headers');
1013 console.log(body);
1014})();
1015```
1016
1017## Comparison
1018
1019| | `got` | `request` | `node-fetch` | `axios` |
1020|-----------------------|:------------:|:------------:|:------------:|:------------:|
1021| HTTP/2 support | ❔ | ✖ | ✖ | ✖ |
1022| Browser support | ✖ | ✖ | ✔* | ✔ |
1023| Electron support | ✔ | ✖ | ✖ | ✖ |
1024| Promise API | ✔ | ✔ | ✔ | ✔ |
1025| Stream API | ✔ | ✔ | Node.js only | ✖ |
1026| Request cancelation | ✔ | ✖ | ✖ | ✔ |
1027| RFC compliant caching | ✔ | ✖ | ✖ | ✖ |
1028| Cookies (out-of-box) | ✔ | ✔ | ✖ | ✖ |
1029| Follows redirects | ✔ | ✔ | ✔ | ✔ |
1030| Retries on failure | ✔ | ✖ | ✖ | ✖ |
1031| Progress events | ✔ | ✖ | ✖ | Browser only |
1032| Handles gzip/deflate | ✔ | ✔ | ✔ | ✔ |
1033| Advanced timeouts | ✔ | ✖ | ✖ | ✖ |
1034| Timings | ✔ | ✔ | ✖ | ✖ |
1035| Errors with metadata | ✔ | ✖ | ✖ | ✔ |
1036| JSON mode | ✔ | ✔ | ✖ | ✔ |
1037| Custom defaults | ✔ | ✔ | ✖ | ✔ |
1038| Composable | ✔ | ✖ | ✖ | ✖ |
1039| Hooks | ✔ | ✖ | ✖ | ✔ |
1040| Issues open | ![][gio] | ![][rio] | ![][nio] | ![][aio] |
1041| Issues closed | ![][gic] | ![][ric] | ![][nic] | ![][aic] |
1042| Downloads | ![][gd] | ![][rd] | ![][nd] | ![][ad] |
1043| Coverage | ![][gc] | ![][rc] | ![][nc] | ![][ac] |
1044| Build | ![][gb] | ![][rb] | ![][nb] | ![][ab] |
1045| Bugs | ![][gbg] | ![][rbg] | ![][nbg] | ![][abg] |
1046| Dependents | ![][gdp] | ![][rdp] | ![][ndp] | ![][adp] |
1047| Install size | ![][gis] | ![][ris] | ![][nis] | ![][ais] |
1048
1049\* It's almost API compatible with the browser `fetch` API.<br>
1050❔ Experimental support.
1051
1052<!-- ISSUES OPEN -->
1053[gio]: https://img.shields.io/github/issues/sindresorhus/got.svg
1054[rio]: https://img.shields.io/github/issues/request/request.svg
1055[nio]: https://img.shields.io/github/issues/bitinn/node-fetch.svg
1056[aio]: https://img.shields.io/github/issues/axios/axios.svg
1057
1058<!-- ISSUES CLOSED -->
1059[gic]: https://img.shields.io/github/issues-closed/sindresorhus/got.svg
1060[ric]: https://img.shields.io/github/issues-closed/request/request.svg
1061[nic]: https://img.shields.io/github/issues-closed/bitinn/node-fetch.svg
1062[aic]: https://img.shields.io/github/issues-closed/axios/axios.svg
1063
1064<!-- DOWNLOADS -->
1065[gd]: https://img.shields.io/npm/dm/got.svg
1066[rd]: https://img.shields.io/npm/dm/request.svg
1067[nd]: https://img.shields.io/npm/dm/node-fetch.svg
1068[ad]: https://img.shields.io/npm/dm/axios.svg
1069
1070<!-- COVERAGE -->
1071[gc]: https://coveralls.io/repos/github/sindresorhus/got/badge.svg?branch=master
1072[rc]: https://coveralls.io/repos/github/request/request/badge.svg?branch=master
1073[nc]: https://coveralls.io/repos/github/bitinn/node-fetch/badge.svg?branch=master
1074[ac]: https://coveralls.io/repos/github/mzabriskie/axios/badge.svg?branch=master
1075
1076<!-- BUILD -->
1077[gb]: https://travis-ci.org/sindresorhus/got.svg?branch=master
1078[rb]: https://travis-ci.org/request/request.svg?branch=master
1079[nb]: https://travis-ci.org/bitinn/node-fetch.svg?branch=master
1080[ab]: https://travis-ci.org/axios/axios.svg?branch=master
1081
1082<!-- BUGS -->
1083[gbg]: https://badgen.net/github/label-issues/sindresorhus/got/bug/open
1084[rbg]: https://badgen.net/github/label-issues/request/request/Needs%20investigation/open
1085[nbg]: https://badgen.net/github/label-issues/bitinn/node-fetch/bug/open
1086[abg]: https://badgen.net/github/label-issues/axios/axios/bug/open
1087
1088<!-- DEPENDENTS -->
1089[gdp]: https://badgen.net/npm/dependents/got
1090[rdp]: https://badgen.net/npm/dependents/request
1091[ndp]: https://badgen.net/npm/dependents/node-fetch
1092[adp]: https://badgen.net/npm/dependents/axios
1093
1094<!-- INSTALL SIZE -->
1095[gis]: https://packagephobia.now.sh/badge?p=got
1096[ris]: https://packagephobia.now.sh/badge?p=request
1097[nis]: https://packagephobia.now.sh/badge?p=node-fetch
1098[ais]: https://packagephobia.now.sh/badge?p=axios
1099
1100
1101## Related
1102
1103- [gh-got](https://github.com/sindresorhus/gh-got) - Got convenience wrapper to interact with the GitHub API
1104- [gl-got](https://github.com/singapore/gl-got) - Got convenience wrapper to interact with the GitLab API
1105- [travis-got](https://github.com/samverschueren/travis-got) - Got convenience wrapper to interact with the Travis API
1106- [graphql-got](https://github.com/kevva/graphql-got) - Got convenience wrapper to interact with GraphQL
1107- [GotQL](https://github.com/khaosdoctor/gotql) - Got convenience wrapper to interact with GraphQL using JSON-parsed queries instead of strings
1108
1109
1110## Maintainers
1111
1112[![Sindre Sorhus](https://github.com/sindresorhus.png?size=100)](https://sindresorhus.com) | [![Vsevolod Strukchinsky](https://github.com/floatdrop.png?size=100)](https://github.com/floatdrop) | [![Alexander Tesfamichael](https://github.com/AlexTes.png?size=100)](https://github.com/AlexTes) | [![Luke Childs](https://github.com/lukechilds.png?size=100)](https://github.com/lukechilds) | [![Szymon Marczak](https://github.com/szmarczak.png?size=100)](https://github.com/szmarczak) | [![Brandon Smith](https://github.com/brandon93s.png?size=100)](https://github.com/brandon93s)
1113---|---|---|---|---|---
1114[Sindre Sorhus](https://sindresorhus.com) | [Vsevolod Strukchinsky](https://github.com/floatdrop) | [Alexander Tesfamichael](https://alextes.me) | [Luke Childs](https://github.com/lukechilds) | [Szymon Marczak](https://github.com/szmarczak) | [Brandon Smith](https://github.com/brandon93s)
1115
1116
1117## License
1118
1119MIT