Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1125 lines
37 KiB

  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. > Simplified HTTP requests
  14. [![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)
  15. Got is a human-friendly and powerful HTTP request library.
  16. It 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)
  17. Got is for Node.js. For browsers, we recommend [Ky](https://github.com/sindresorhus/ky).
  18. ## Highlights
  19. - [Promise & stream API](#api)
  20. - [Request cancelation](#aborting-the-request)
  21. - [RFC compliant caching](#cache-adapters)
  22. - [Follows redirects](#followredirect)
  23. - [Retries on failure](#retry)
  24. - [Progress events](#onuploadprogress-progress)
  25. - [Handles gzip/deflate](#decompress)
  26. - [Timeout handling](#timeout)
  27. - [Errors with metadata](#errors)
  28. - [JSON mode](#json)
  29. - [WHATWG URL support](#url)
  30. - [Hooks](https://github.com/sindresorhus/got#hooks)
  31. - [Instances with custom defaults](#instances)
  32. - [Composable](advanced-creation.md#merging-instances)
  33. - [Electron support](#useelectronnet)
  34. - [Used by ~2000 packages and ~500K repos](https://github.com/sindresorhus/got/network/dependents)
  35. - Actively maintained
  36. [See how Got compares to other HTTP libraries](#comparison)
  37. ## Install
  38. ```
  39. $ npm install got
  40. ```
  41. <a href="https://www.patreon.com/sindresorhus">
  42. <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
  43. </a>
  44. ## Usage
  45. ```js
  46. const got = require('got');
  47. (async () => {
  48. try {
  49. const response = await got('sindresorhus.com');
  50. console.log(response.body);
  51. //=> '<!doctype html> ...'
  52. } catch (error) {
  53. console.log(error.response.body);
  54. //=> 'Internal server error ...'
  55. }
  56. })();
  57. ```
  58. ###### Streams
  59. ```js
  60. const fs = require('fs');
  61. const got = require('got');
  62. got.stream('sindresorhus.com').pipe(fs.createWriteStream('index.html'));
  63. // For POST, PUT, and PATCH methods `got.stream` returns a `stream.Writable`
  64. fs.createReadStream('index.html').pipe(got.stream.post('sindresorhus.com'));
  65. ```
  66. ### API
  67. It's a `GET` request by default, but can be changed by using different methods or in the `options`.
  68. #### got(url, [options])
  69. Returns a Promise for a [`response` object](#response) or a [stream](#streams-1) if `options.stream` is set to true.
  70. ##### url
  71. Type: `string` `Object`
  72. The 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).
  73. Properties from `options` will override properties in the parsed `url`.
  74. If no protocol is specified, it will default to `https`.
  75. ##### options
  76. Type: `Object`
  77. Any of the [`https.request`](https://nodejs.org/api/https.html#https_https_request_options_callback) options.
  78. ###### baseUrl
  79. Type: `string` `Object`
  80. When specified, `url` will be prepended by `baseUrl`.<br>
  81. If you specify an absolute URL, it will skip the `baseUrl`.
  82. Very useful when used with `got.extend()` to create niche-specific Got instances.
  83. Can be a string or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
  84. Slash at the end of `baseUrl` and at the beginning of the `url` argument is optional:
  85. ```js
  86. await got('hello', {baseUrl: 'https://example.com/v1'});
  87. //=> 'https://example.com/v1/hello'
  88. await got('/hello', {baseUrl: 'https://example.com/v1/'});
  89. //=> 'https://example.com/v1/hello'
  90. await got('/hello', {baseUrl: 'https://example.com/v1'});
  91. //=> 'https://example.com/v1/hello'
  92. ```
  93. ###### headers
  94. Type: `Object`<br>
  95. Default: `{}`
  96. Request headers.
  97. Existing headers will be overwritten. Headers set to `null` will be omitted.
  98. ###### stream
  99. Type: `boolean`<br>
  100. Default: `false`
  101. Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.stream(url, [options])`.
  102. ###### body
  103. Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data)
  104. *If you provide this option, `got.stream()` will be read-only.*
  105. The body that will be sent with a `POST` request.
  106. If present in `options` and `options.method` is not set, `options.method` will be set to `POST`.
  107. The `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`.
  108. ###### cookieJar
  109. Type: [`tough.CookieJar` instance](https://github.com/salesforce/tough-cookie#cookiejar)
  110. Cookie support. You don't have to care about parsing or how to store them. [Example.](#cookies)
  111. **Note:** `options.headers.cookie` will be overridden.
  112. ###### encoding
  113. Type: `string` `null`<br>
  114. Default: `'utf8'`
  115. [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).
  116. ###### form
  117. Type: `boolean`<br>
  118. Default: `false`
  119. *If you provide this option, `got.stream()` will be read-only.*
  120. If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`.
  121. `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).
  122. ###### json
  123. Type: `boolean`<br>
  124. Default: `false`
  125. *If you use `got.stream()`, this option will be ignored.*
  126. If set to `true` and `Content-Type` header is not set, it will be set to `application/json`.
  127. Parse 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.
  128. `body` must be a plain object or array and will be stringified.
  129. ###### query
  130. Type: `string` `Object<string, string|number>` [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
  131. Query string that will be added to the request URL. This will override the query string in `url`.
  132. If you need to pass in an array, you can do it using a `URLSearchParams` instance:
  133. ```js
  134. const got = require('got');
  135. const query = new URLSearchParams([['key', 'a'], ['key', 'b']]);
  136. got('https://example.com', {query});
  137. console.log(query.toString());
  138. //=> 'key=a&key=b'
  139. ```
  140. And if you need a different array format, you could use the [`query-string`](https://github.com/sindresorhus/query-string) package:
  141. ```js
  142. const got = require('got');
  143. const queryString = require('query-string');
  144. const query = queryString.stringify({key: ['a', 'b']}, {arrayFormat: 'bracket'});
  145. got('https://example.com', {query});
  146. console.log(query);
  147. //=> 'key[]=a&key[]=b'
  148. ```
  149. ###### timeout
  150. Type: `number` `Object`
  151. Milliseconds 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.
  152. This also accepts an `object` with the following fields to constrain the duration of each phase of the request lifecycle:
  153. - `lookup` starts when a socket is assigned and ends when the hostname has been resolved. Does not apply when using a Unix domain socket.
  154. - `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.
  155. - `secureConnect` starts when `connect` completes and ends when the handshaking process completes (HTTPS only).
  156. - `socket` starts when the socket is connected. See [request.setTimeout](https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback).
  157. - `response` starts when the request has been written to the socket and ends when the response headers are received.
  158. - `send` starts when the socket is connected and ends with the request has been written to the socket.
  159. - `request` starts when the request is initiated and ends when the response's end event fires.
  160. ###### retry
  161. Type: `number` `Object`<br>
  162. Default:
  163. - retries: `2`
  164. - methods: `GET` `PUT` `HEAD` `DELETE` `OPTIONS` `TRACE`
  165. - 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)
  166. - maxRetryAfter: `undefined`
  167. An 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.
  168. If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`.<br>
  169. If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
  170. Delays between retries counts with function `1000 * Math.pow(2, retry) + Math.random() * 100`, where `retry` is attempt number (starts from 1).
  171. The `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).
  172. **Note:** It retries only on the specified methods, status codes, and on these network errors:
  173. - `ETIMEDOUT`: One of the [timeout](#timeout) limits were reached.
  174. - `ECONNRESET`: Connection was forcibly closed by a peer.
  175. - `EADDRINUSE`: Could not bind to any free port.
  176. - `ECONNREFUSED`: Connection was refused by the server.
  177. - `EPIPE`: The remote side of the stream being written has been closed.
  178. ###### followRedirect
  179. Type: `boolean`<br>
  180. Default: `true`
  181. Defines if redirect responses should be followed automatically.
  182. Note 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).
  183. ###### decompress
  184. Type: `boolean`<br>
  185. Default: `true`
  186. Decompress the response automatically. This will set the `accept-encoding` header to `gzip, deflate` unless you set it yourself.
  187. If 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.
  188. ###### cache
  189. Type: `Object`<br>
  190. Default: `false`
  191. [Cache adapter instance](#cache-adapters) for storing cached data.
  192. ###### request
  193. Type: `Function`<br>
  194. Default: `http.request` `https.request` *(depending on the protocol)*
  195. Custom request function. The main purpose of this is to [support HTTP2 using a wrapper](#experimental-http2-support).
  196. ###### useElectronNet
  197. Type: `boolean`<br>
  198. Default: `false`
  199. When 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).
  200. ###### throwHttpErrors
  201. Type: `boolean`<br>
  202. Default: `true`
  203. Determines if a `got.HTTPError` is thrown for error responses (non-2xx status codes).
  204. If 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.
  205. ###### agent
  206. Same as the [`agent` option](https://nodejs.org/api/http.html#http_http_request_url_options_callback) for `http.request`, but with an extra feature:
  207. If 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.
  208. ```js
  209. const got = require('got');
  210. const HttpAgent = require('agentkeepalive');
  211. const {HttpsAgent} = HttpAgent;
  212. got('sindresorhus.com', {
  213. agent: {
  214. http: new HttpAgent(),
  215. https: new HttpsAgent()
  216. }
  217. });
  218. ```
  219. ###### hooks
  220. Type: `Object<string, Function[]>`
  221. Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially.
  222. ###### hooks.beforeRequest
  223. Type: `Function[]`<br>
  224. Default: `[]`
  225. Called 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.
  226. See the [AWS section](#aws) for an example.
  227. **Note**: If you modify the `body` you will need to modify the `content-length` header too, because it has already been computed and assigned.
  228. ###### hooks.beforeRedirect
  229. Type: `Function[]`<br>
  230. Default: `[]`
  231. Called 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:
  232. ```js
  233. const got = require('got');
  234. got('example.com', {
  235. hooks: {
  236. beforeRedirect: [
  237. options => {
  238. if (options.hostname === 'deadSite') {
  239. options.hostname = 'fallbackSite';
  240. }
  241. }
  242. ]
  243. }
  244. });
  245. ```
  246. ###### hooks.beforeRetry
  247. Type: `Function[]`<br>
  248. Default: `[]`
  249. Called 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:
  250. ```js
  251. const got = require('got');
  252. got('example.com', {
  253. hooks: {
  254. beforeRetry: [
  255. (options, error, retryCount) => {
  256. if (error.statusCode === 413) { // Payload too large
  257. options.body = getNewBody();
  258. }
  259. }
  260. ]
  261. }
  262. });
  263. ```
  264. ###### hooks.afterResponse
  265. Type: `Function[]`<br>
  266. Default: `[]`
  267. Called with [response object](#response) and a retry function.
  268. Each function should return the response. This is especially useful when you want to refresh an access token. Example:
  269. ```js
  270. const got = require('got');
  271. const instance = got.extend({
  272. hooks: {
  273. afterResponse: [
  274. (response, retryWithMergedOptions) => {
  275. if (response.statusCode === 401) { // Unauthorized
  276. const updatedOptions = {
  277. headers: {
  278. token: getNewToken() // Refresh the access token
  279. }
  280. };
  281. // Save for further requests
  282. instance.defaults.options = got.mergeOptions(instance.defaults.options, updatedOptions);
  283. // Make a new retry
  284. return retryWithMergedOptions(updatedOptions);
  285. }
  286. // No changes otherwise
  287. return response;
  288. }
  289. ]
  290. },
  291. mutableDefaults: true
  292. });
  293. ```
  294. #### Response
  295. The 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.
  296. ##### body
  297. Type: `string` `Object` *(depending on `options.json`)*
  298. The result of the request.
  299. ##### url
  300. Type: `string`
  301. The request URL or the final URL after redirects.
  302. ##### requestUrl
  303. Type: `string`
  304. The original request URL.
  305. ##### timings
  306. Type: `Object`
  307. The object contains the following properties:
  308. - `start` - Time when the request started.
  309. - `socket` - Time when a socket was assigned to the request.
  310. - `lookup` - Time when the DNS lookup finished.
  311. - `connect` - Time when the socket successfully connected.
  312. - `upload` - Time when the request finished uploading.
  313. - `response` - Time when the request fired the `response` event.
  314. - `end` - Time when the response fired the `end` event.
  315. - `error` - Time when the request fired the `error` event.
  316. - `phases`
  317. - `wait` - `timings.socket - timings.start`
  318. - `dns` - `timings.lookup - timings.socket`
  319. - `tcp` - `timings.connect - timings.lookup`
  320. - `request` - `timings.upload - timings.connect`
  321. - `firstByte` - `timings.response - timings.upload`
  322. - `download` - `timings.end - timings.response`
  323. - `total` - `timings.end - timings.start` or `timings.error - timings.start`
  324. **Note**: The time is a `number` representing the milliseconds elapsed since the UNIX epoch.
  325. ##### fromCache
  326. Type: `boolean`
  327. Whether the response was retrieved from the cache.
  328. ##### redirectUrls
  329. Type: `Array`
  330. The redirect URLs.
  331. ##### retryCount
  332. Type: `number`
  333. The number of times the request was retried.
  334. #### Streams
  335. **Note**: Progress events, redirect events and request/response events can also be used with promises.
  336. #### got.stream(url, [options])
  337. Sets `options.stream` to `true`.
  338. Returns a [duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with additional events:
  339. ##### .on('request', request)
  340. `request` event to get the request object of the request.
  341. **Tip**: You can use `request` event to abort request:
  342. ```js
  343. got.stream('github.com')
  344. .on('request', request => setTimeout(() => request.abort(), 50));
  345. ```
  346. ##### .on('response', response)
  347. The `response` event to get the response object of the final request.
  348. ##### .on('redirect', response, nextOptions)
  349. The `redirect` event to get the response object of a redirect. The second argument is options for the next request to the redirect location.
  350. ##### .on('uploadProgress', progress)
  351. ##### .on('downloadProgress', progress)
  352. Progress events for uploading (sending a request) and downloading (receiving a response). The `progress` argument is an object like:
  353. ```js
  354. {
  355. percent: 0.1,
  356. transferred: 1024,
  357. total: 10240
  358. }
  359. ```
  360. If it's not possible to retrieve the body size (can happen when streaming), `total` will be `null`.
  361. ```js
  362. (async () => {
  363. const response = await got('sindresorhus.com')
  364. .on('downloadProgress', progress => {
  365. // Report download progress
  366. })
  367. .on('uploadProgress', progress => {
  368. // Report upload progress
  369. });
  370. console.log(response);
  371. })();
  372. ```
  373. ##### .on('error', error, body, response)
  374. The `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.
  375. #### got.get(url, [options])
  376. #### got.post(url, [options])
  377. #### got.put(url, [options])
  378. #### got.patch(url, [options])
  379. #### got.head(url, [options])
  380. #### got.delete(url, [options])
  381. Sets `options.method` to the method name and makes a request.
  382. ### Instances
  383. #### got.extend([options])
  384. Configure a new `got` instance with default `options`. The `options` are merged with the parent instance's `defaults.options` using [`got.mergeOptions`](#gotmergeoptionsparentoptions-newoptions). You can access the resolved options with the `.defaults` property on the instance.
  385. ```js
  386. const client = got.extend({
  387. baseUrl: 'https://example.com',
  388. headers: {
  389. 'x-unicorn': 'rainbow'
  390. }
  391. });
  392. client.get('/demo');
  393. /* HTTP Request =>
  394. * GET /demo HTTP/1.1
  395. * Host: example.com
  396. * x-unicorn: rainbow
  397. */
  398. ```
  399. ```js
  400. (async () => {
  401. const client = got.extend({
  402. baseUrl: 'httpbin.org',
  403. headers: {
  404. 'x-foo': 'bar'
  405. }
  406. });
  407. const {headers} = (await client.get('/headers', {json: true})).body;
  408. //=> headers['x-foo'] === 'bar'
  409. const jsonClient = client.extend({
  410. json: true,
  411. headers: {
  412. 'x-baz': 'qux'
  413. }
  414. });
  415. const {headers: headers2} = (await jsonClient.get('/headers')).body;
  416. //=> headers2['x-foo'] === 'bar'
  417. //=> headers2['x-baz'] === 'qux'
  418. })();
  419. ```
  420. *Need more control over the behavior of Got? Check out the [`got.create()`](advanced-creation.md).*
  421. #### got.mergeOptions(parentOptions, newOptions)
  422. Extends 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:
  423. ```js
  424. const a = {headers: {cat: 'meow', wolf: ['bark', 'wrrr']}};
  425. const b = {headers: {cow: 'moo', wolf: ['auuu']}};
  426. {...a, ...b} // => {headers: {cow: 'moo', wolf: ['auuu']}}
  427. got.mergeOptions(a, b) // => {headers: {cat: 'meow', cow: 'moo', wolf: ['auuu']}}
  428. ```
  429. Options are deeply merged to a new object. The value of each key is determined as follows:
  430. - If the new property is set to `undefined`, it keeps the old one.
  431. - 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).
  432. - If the new property is a plain `Object`:
  433. - If the parent property is a plain `Object` too, both values are merged recursively into a new `Object`.
  434. - Otherwise, only the new value is deeply cloned.
  435. - If the new property is an `Array`, it overwrites the old one with a deep clone of the new property.
  436. - Otherwise, the new value is assigned to the key.
  437. #### got.defaults
  438. Type: `Object`
  439. The default Got options.
  440. ## Errors
  441. Each error contains (if available) `body`, `statusCode`, `statusMessage`, `host`, `hostname`, `method`, `path`, `protocol` and `url` properties to make debugging easier.
  442. In Promise mode, the `response` is attached to the error.
  443. #### got.CacheError
  444. When a cache method fails, for example, if the database goes down or there's a filesystem error.
  445. #### got.RequestError
  446. When a request fails. Contains a `code` property with error class code, like `ECONNREFUSED`.
  447. #### got.ReadError
  448. When reading from response stream fails.
  449. #### got.ParseError
  450. When `json` option is enabled, server response code is 2xx, and `JSON.parse` fails.
  451. #### got.HTTPError
  452. When the server response code is not 2xx. Includes `statusCode`, `statusMessage`, and `redirectUrls` properties.
  453. #### got.MaxRedirectsError
  454. When 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.
  455. #### got.UnsupportedProtocolError
  456. When given an unsupported protocol.
  457. #### got.CancelError
  458. When the request is aborted with `.cancel()`.
  459. #### got.TimeoutError
  460. When the request is aborted due to a [timeout](#timeout)
  461. ## Aborting the request
  462. The promise returned by Got has a [`.cancel()`](https://github.com/sindresorhus/p-cancelable) method which when called, aborts the request.
  463. ```js
  464. (async () => {
  465. const request = got(url, options);
  466. // …
  467. // In another part of the code
  468. if (something) {
  469. request.cancel();
  470. }
  471. // …
  472. try {
  473. await request;
  474. } catch (error) {
  475. if (request.isCanceled) { // Or `error instanceof got.CancelError`
  476. // Handle cancelation
  477. }
  478. // Handle other errors
  479. }
  480. })();
  481. ```
  482. <a name="cache-adapters"></a>
  483. ## Cache
  484. Got 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).
  485. You can use the JavaScript `Map` type as an in-memory cache:
  486. ```js
  487. const got = require('got');
  488. const map = new Map();
  489. (async () => {
  490. let response = await got('sindresorhus.com', {cache: map});
  491. console.log(response.fromCache);
  492. //=> false
  493. response = await got('sindresorhus.com', {cache: map});
  494. console.log(response.fromCache);
  495. //=> true
  496. })();
  497. ```
  498. Got 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):
  499. ```
  500. $ npm install @keyv/redis
  501. ```
  502. ```js
  503. const got = require('got');
  504. const KeyvRedis = require('@keyv/redis');
  505. const redis = new KeyvRedis('redis://user:pass@localhost:6379');
  506. got('sindresorhus.com', {cache: redis});
  507. ```
  508. Got supports anything that follows the Map API, so it's easy to write your own storage adapter or use a third-party solution.
  509. For example, the following are all valid storage adapters:
  510. ```js
  511. const storageAdapter = new Map();
  512. // Or
  513. const storageAdapter = require('./my-storage-adapter');
  514. // Or
  515. const QuickLRU = require('quick-lru');
  516. const storageAdapter = new QuickLRU({maxSize: 1000});
  517. got('sindresorhus.com', {cache: storageAdapter});
  518. ```
  519. View the [Keyv docs](https://github.com/lukechilds/keyv) for more information on how to use storage adapters.
  520. ## Proxies
  521. You can use the [`tunnel`](https://github.com/koichik/node-tunnel) package with the `agent` option to work with proxies:
  522. ```js
  523. const got = require('got');
  524. const tunnel = require('tunnel');
  525. got('sindresorhus.com', {
  526. agent: tunnel.httpOverHttp({
  527. proxy: {
  528. host: 'localhost'
  529. }
  530. })
  531. });
  532. ```
  533. Check 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.
  534. ## Cookies
  535. You can use the [`tough-cookie`](https://github.com/salesforce/tough-cookie) package:
  536. ```js
  537. const got = require('got');
  538. const {CookieJar} = require('tough-cookie');
  539. const cookieJar = new CookieJar();
  540. cookieJar.setCookie('foo=bar', 'https://www.google.com');
  541. got('google.com', {cookieJar});
  542. ```
  543. ## Form data
  544. You can use the [`form-data`](https://github.com/form-data/form-data) package to create POST request with form data:
  545. ```js
  546. const fs = require('fs');
  547. const got = require('got');
  548. const FormData = require('form-data');
  549. const form = new FormData();
  550. form.append('my_file', fs.createReadStream('/foo/bar.jpg'));
  551. got.post('google.com', {
  552. body: form
  553. });
  554. ```
  555. ## OAuth
  556. You can use the [`oauth-1.0a`](https://github.com/ddo/oauth-1.0a) package to create a signed OAuth request:
  557. ```js
  558. const got = require('got');
  559. const crypto = require('crypto');
  560. const OAuth = require('oauth-1.0a');
  561. const oauth = OAuth({
  562. consumer: {
  563. key: process.env.CONSUMER_KEY,
  564. secret: process.env.CONSUMER_SECRET
  565. },
  566. signature_method: 'HMAC-SHA1',
  567. hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
  568. });
  569. const token = {
  570. key: process.env.ACCESS_TOKEN,
  571. secret: process.env.ACCESS_TOKEN_SECRET
  572. };
  573. const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
  574. got(url, {
  575. headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
  576. json: true
  577. });
  578. ```
  579. ## Unix Domain Sockets
  580. Requests 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`.
  581. - `PROTOCOL` - `http` or `https` *(optional)*
  582. - `SOCKET` - Absolute path to a unix domain socket, for example: `/var/run/docker.sock`
  583. - `PATH` - Request path, for example: `/v2/keys`
  584. ```js
  585. got('http://unix:/var/run/docker.sock:/containers/json');
  586. // Or without protocol (HTTP by default)
  587. got('unix:/var/run/docker.sock:/containers/json');
  588. ```
  589. ## AWS
  590. Requests 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.
  591. ```js
  592. const AWS = require('aws-sdk');
  593. const aws4 = require('aws4');
  594. const got = require('got');
  595. const chain = new AWS.CredentialProviderChain();
  596. // Create a Got instance to use relative paths and signed requests
  597. const awsClient = got.extend({
  598. baseUrl: 'https://<api-id>.execute-api.<api-region>.amazonaws.com/<stage>/',
  599. hooks: {
  600. beforeRequest: [
  601. async options => {
  602. const credentials = await chain.resolvePromise();
  603. aws4.sign(options, credentials);
  604. }
  605. ]
  606. }
  607. });
  608. const response = await awsClient('endpoint/path', {
  609. // Request-specific options
  610. });
  611. ```
  612. ## Testing
  613. You can test your requests by using the [`nock`](https://github.com/node-nock/nock) package to mock an endpoint:
  614. ```js
  615. const got = require('got');
  616. const nock = require('nock');
  617. nock('https://sindresorhus.com')
  618. .get('/')
  619. .reply(200, 'Hello world!');
  620. (async () => {
  621. const response = await got('sindresorhus.com');
  622. console.log(response.body);
  623. //=> 'Hello world!'
  624. })();
  625. ```
  626. If you need real integration tests you can use [`create-test-server`](https://github.com/lukechilds/create-test-server):
  627. ```js
  628. const got = require('got');
  629. const createTestServer = require('create-test-server');
  630. (async () => {
  631. const server = await createTestServer();
  632. server.get('/', 'Hello world!');
  633. const response = await got(server.url);
  634. console.log(response.body);
  635. //=> 'Hello world!'
  636. await server.close();
  637. })();
  638. ```
  639. ## Tips
  640. ### User Agent
  641. 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`.
  642. ```js
  643. const got = require('got');
  644. const pkg = require('./package.json');
  645. got('sindresorhus.com', {
  646. headers: {
  647. 'user-agent': `my-package/${pkg.version} (https://github.com/username/my-package)`
  648. }
  649. });
  650. got('sindresorhus.com', {
  651. headers: {
  652. 'user-agent': null
  653. }
  654. });
  655. ```
  656. ### 304 Responses
  657. Bear 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.
  658. ### Custom endpoints
  659. Use `got.extend()` to make it nicer to work with REST APIs. Especially if you use the `baseUrl` option.
  660. **Note:** Not to be confused with [`got.create()`](advanced-creation.md), which has no defaults.
  661. ```js
  662. const got = require('got');
  663. const pkg = require('./package.json');
  664. const custom = got.extend({
  665. baseUrl: 'example.com',
  666. json: true,
  667. headers: {
  668. 'user-agent': `my-package/${pkg.version} (https://github.com/username/my-package)`
  669. }
  670. });
  671. // Use `custom` exactly how you use `got`
  672. (async () => {
  673. const list = await custom('/v1/users/list');
  674. })();
  675. ```
  676. *Need to merge some instances into a single one? Check out [`got.mergeInstances()`](advanced-creation.md#merging-instances).*
  677. ### Experimental HTTP2 support
  678. Got provides an experimental support for HTTP2 using the [`http2-wrapper`](https://github.com/szmarczak/http2-wrapper) package:
  679. ```js
  680. const got = require('got');
  681. const {request} = require('http2-wrapper');
  682. const h2got = got.extend({request});
  683. (async () => {
  684. const {body} = await h2got('https://nghttp2.org/httpbin/headers');
  685. console.log(body);
  686. })();
  687. ```
  688. ## Comparison
  689. | | `got` | `request` | `node-fetch` | `axios` |
  690. |-----------------------|:------------:|:------------:|:------------:|:------------:|
  691. | HTTP/2 support | ❔ | ✖ | ✖ | ✖ |
  692. | Browser support | ✖ | ✖ | ✔* | ✔ |
  693. | Electron support | ✔ | ✖ | ✖ | ✖ |
  694. | Promise API | ✔ | ✔ | ✔ | ✔ |
  695. | Stream API | ✔ | ✔ | Node.js only | ✖ |
  696. | Request cancelation | ✔ | ✖ | ✖ | ✔ |
  697. | RFC compliant caching | ✔ | ✖ | ✖ | ✖ |
  698. | Cookies (out-of-box) | ✔ | ✔ | ✖ | ✖ |
  699. | Follows redirects | ✔ | ✔ | ✔ | ✔ |
  700. | Retries on failure | ✔ | ✖ | ✖ | ✖ |
  701. | Progress events | ✔ | ✖ | ✖ | Browser only |
  702. | Handles gzip/deflate | ✔ | ✔ | ✔ | ✔ |
  703. | Advanced timeouts | ✔ | ✖ | ✖ | ✖ |
  704. | Timings | ✔ | ✔ | ✖ | ✖ |
  705. | Errors with metadata | ✔ | ✖ | ✖ | ✔ |
  706. | JSON mode | ✔ | ✔ | ✖ | ✔ |
  707. | Custom defaults | ✔ | ✔ | ✖ | ✔ |
  708. | Composable | ✔ | ✖ | ✖ | ✖ |
  709. | Hooks | ✔ | ✖ | ✖ | ✔ |
  710. | Issues open | ![][gio] | ![][rio] | ![][nio] | ![][aio] |
  711. | Issues closed | ![][gic] | ![][ric] | ![][nic] | ![][aic] |
  712. | Downloads | ![][gd] | ![][rd] | ![][nd] | ![][ad] |
  713. | Coverage | ![][gc] | ![][rc] | ![][nc] | ![][ac] |
  714. | Build | ![][gb] | ![][rb] | ![][nb] | ![][ab] |
  715. | Bugs | ![][gbg] | ![][rbg] | ![][nbg] | ![][abg] |
  716. | Dependents | ![][gdp] | ![][rdp] | ![][ndp] | ![][adp] |
  717. | Install size | ![][gis] | ![][ris] | ![][nis] | ![][ais] |
  718. \* It's almost API compatible with the browser `fetch` API.<br>
  719. ❔ Experimental support.
  720. <!-- ISSUES OPEN -->
  721. [gio]: https://img.shields.io/github/issues/sindresorhus/got.svg
  722. [rio]: https://img.shields.io/github/issues/request/request.svg
  723. [nio]: https://img.shields.io/github/issues/bitinn/node-fetch.svg
  724. [aio]: https://img.shields.io/github/issues/axios/axios.svg
  725. <!-- ISSUES CLOSED -->
  726. [gic]: https://img.shields.io/github/issues-closed/sindresorhus/got.svg
  727. [ric]: https://img.shields.io/github/issues-closed/request/request.svg
  728. [nic]: https://img.shields.io/github/issues-closed/bitinn/node-fetch.svg
  729. [aic]: https://img.shields.io/github/issues-closed/axios/axios.svg
  730. <!-- DOWNLOADS -->
  731. [gd]: https://img.shields.io/npm/dm/got.svg
  732. [rd]: https://img.shields.io/npm/dm/request.svg
  733. [nd]: https://img.shields.io/npm/dm/node-fetch.svg
  734. [ad]: https://img.shields.io/npm/dm/axios.svg
  735. <!-- COVERAGE -->
  736. [gc]: https://coveralls.io/repos/github/sindresorhus/got/badge.svg?branch=master
  737. [rc]: https://coveralls.io/repos/github/request/request/badge.svg?branch=master
  738. [nc]: https://coveralls.io/repos/github/bitinn/node-fetch/badge.svg?branch=master
  739. [ac]: https://coveralls.io/repos/github/mzabriskie/axios/badge.svg?branch=master
  740. <!-- BUILD -->
  741. [gb]: https://travis-ci.org/sindresorhus/got.svg?branch=master
  742. [rb]: https://travis-ci.org/request/request.svg?branch=master
  743. [nb]: https://travis-ci.org/bitinn/node-fetch.svg?branch=master
  744. [ab]: https://travis-ci.org/axios/axios.svg?branch=master
  745. <!-- BUGS -->
  746. [gbg]: https://badgen.net/github/label-issues/sindresorhus/got/bug/open
  747. [rbg]: https://badgen.net/github/label-issues/request/request/Needs%20investigation/open
  748. [nbg]: https://badgen.net/github/label-issues/bitinn/node-fetch/bug/open
  749. [abg]: https://badgen.net/github/label-issues/axios/axios/bug/open
  750. <!-- DEPENDENTS -->
  751. [gdp]: https://badgen.net/npm/dependents/got
  752. [rdp]: https://badgen.net/npm/dependents/request
  753. [ndp]: https://badgen.net/npm/dependents/node-fetch
  754. [adp]: https://badgen.net/npm/dependents/axios
  755. <!-- INSTALL SIZE -->
  756. [gis]: https://packagephobia.now.sh/badge?p=got
  757. [ris]: https://packagephobia.now.sh/badge?p=request
  758. [nis]: https://packagephobia.now.sh/badge?p=node-fetch
  759. [ais]: https://packagephobia.now.sh/badge?p=axios
  760. ## Related
  761. - [gh-got](https://github.com/sindresorhus/gh-got) - Got convenience wrapper to interact with the GitHub API
  762. - [gl-got](https://github.com/singapore/gl-got) - Got convenience wrapper to interact with the GitLab API
  763. - [travis-got](https://github.com/samverschueren/travis-got) - Got convenience wrapper to interact with the Travis API
  764. - [graphql-got](https://github.com/kevva/graphql-got) - Got convenience wrapper to interact with GraphQL
  765. - [GotQL](https://github.com/khaosdoctor/gotql) - Got convenience wrapper to interact with GraphQL using JSON-parsed queries instead of strings
  766. ## Maintainers
  767. [![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)
  768. ---|---|---|---|---|---
  769. [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)
  770. ## License
  771. MIT