<div align="center">
<br>
<br>
<img width="360" src="media/logo.svg" alt="Got">
<br>
<br>
<br>
<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!
</p>
<br>
<br>
</div>
> Simplified HTTP requests
[![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)
Got is a human-friendly and powerful HTTP request library.
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)
Got is for Node.js. For browsers, we recommend [Ky](https://github.com/sindresorhus/ky).
## Highlights
- [Promise & stream API](#api)
- [Request cancelation](#aborting-the-request)
- [RFC compliant caching](#cache-adapters)
- [Follows redirects](#followredirect)
- [Retries on failure](#retry)
- [Progress events](#onuploadprogress-progress)
- [Handles gzip/deflate](#decompress)
- [Timeout handling](#timeout)
- [Errors with metadata](#errors)
- [JSON mode](#json)
- [WHATWG URL support](#url)
- [Hooks](#hooks)
- [Instances with custom defaults](#instances)
- [Composable](advanced-creation.md#merging-instances)
- [Electron support](#useelectronnet)
- [Used by ~2000 packages and ~500K repos](https://github.com/sindresorhus/got/network/dependents)
- Actively maintained
[Moving from Request?](migration-guides.md)
[See how Got compares to other HTTP libraries](#comparison)
## Install
```
$ npm install got
```
<a href="https://www.patreon.com/sindresorhus">
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
</a>
## Usage
```js
const got = require('got');
(async () => {
try {
const response = await got('sindresorhus.com');
console.log(response.body);
//=> '<!doctype html> ...'
} catch (error) {
console.log(error.response.body);
//=> 'Internal server error ...'
}
})();
```
###### Streams
```js
const fs = require('fs');
const got = require('got');
got.stream('sindresorhus.com').pipe(fs.createWriteStream('index.html'));
// For POST, PUT, and PATCH methods `got.stream` returns a `stream.Writable`
fs.createReadStream('index.html').pipe(got.stream.post('sindresorhus.com'));
```
### API
It's a `GET` request by default, but can be changed by using different methods or in the `options`.
#### got(url, [options])
Returns a Promise for a [`response` object](#response) or a [stream](#streams-1) if `options.stream` is set to true.
##### url
Type: `string` `Object`
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).
Properties from `options` will override properties in the parsed `url`.
If no protocol is specified, it will default to `https`.
##### options
Type: `Object`
Any of the [`https.request`](https://nodejs.org/api/https.html#https_https_request_options_callback) options.
###### baseUrl
Type: `string` `Object`
When specified, `url` will be prepended by `baseUrl`.<br>
If you specify an absolute URL, it will skip the `baseUrl`.
Very useful when used with `got.extend()` to create niche-specific Got instances.
Can be a string or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
Slash at the end of `baseUrl` and at the beginning of the `url` argument is optional:
```js
await got('hello', {baseUrl: 'https://example.com/v1'});
//=> 'https://example.com/v1/hello'
await got('/hello', {baseUrl: 'https://example.com/v1/'});
//=> 'https://example.com/v1/hello'
await got('/hello', {baseUrl: 'https://example.com/v1'});
//=> 'https://example.com/v1/hello'
```
###### headers
Type: `Object`<br>
Default: `{}`
Request headers.
Existing headers will be overwritten. Headers set to `null` will be omitted.
###### stream
Type: `boolean`<br>
Default: `false`
Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.stream(url, [options])`.
###### body
Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data)
**Note:** If you provide this option, `got.stream()` will be read-only.
The body that will be sent with a `POST` request.
If present in `options` and `options.method` is not set, `options.method` will be set to `POST`.
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`.
###### cookieJar
Type: [`tough.CookieJar` instance](https://github.com/salesforce/tough-cookie#cookiejar)
**Note:** If you provide this option, `options.headers.cookie` will be overridden.
Cookie support. You don't have to care about parsing or how to store them. [Example.](#cookies)
###### encoding
Type: `string` `null`<br>
Default: `'utf8'`
[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).
###### form
Type: `boolean`<br>
Default: `false`
**Note:** If you provide this option, `got.stream()` will be read-only.
**Note:** `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).
If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`.
###### json
Type: `boolean`<br>
Default: `false`
**Note:** If you use `got.stream()`, this option will be ignored.
**Note:** `body` must be a plain object or array and will be stringified.
If set to `true` and `Content-Type` header is not set, it will be set to `application/json`.
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.
###### query
Type: `string` `Object<string, string|number>` [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
Query string that will be added to the request URL. This will override the query string in `url`.
If you need to pass in an array, you can do it using a `URLSearchParams` instance:
```js
const got = require('got');
const query = new URLSearchParams([['key', 'a'], ['key', 'b']]);
got('https://example.com', {query});
console.log(query.toString());
//=> 'key=a&key=b'
```
And if you need a different array format, you could use the [`query-string`](https://github.com/sindresorhus/query-string) package:
```js
const got = require('got');
const queryString = require('query-string');
const query = queryString.stringify({key: ['a', 'b']}, {arrayFormat: 'bracket'});
got('https://example.com', {query});
console.log(query);
//=> 'key[]=a&key[]=b'
```
###### timeout
Type: `number` `Object`
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.
This also accepts an `object` with the following fields to constrain the duration of each phase of the request lifecycle:
- `lookup` starts when a socket is assigned and ends when the hostname has been resolved