body

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, and content-length and transfer-encoding are not manually set in options.headers.

encoding

Type: stringnull
Default: 'utf8'

Encoding to be used on setEncoding of the response data. If null, the body is returned as a Buffer (binary data).

form

Type: boolean
Default: false

If you provide this option, got.stream() will be read-only.

If set to true and Content-Type header is not set, it will be set to application/x-www-form-urlencoded.

json

Type: boolean
Default: false

If you use got.stream(), this option will be ignored.

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.

body must be a plain object or array and will be stringified.

query

Type: stringObject

Query string object that will be added to the request URL. This will override the query string in url.

timeout

Type: numberObject

Milliseconds to wait for the server to end the response before aborting the request with got.TimeoutError 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. Does not apply when using a Unix domain socket.

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.

followRedirect

Type: boolean
Default: true

Defines if redirect responses should be followed automatically.

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.

decompress

Type: boolean
Default: true

Decompress the response automatically. This will set the accept-encoding header to gzip, deflate unless you set it yourself.

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.

cache

useElectronNet

When used in Electron, Got will use electron.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 and #461.

throwHttpErrors

Type: boolean
Default: true

Determines if a got.HTTPError is thrown for error responses (non-2xx status codes).

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.

hooks

Type: Object<string, Function[]>
Default: {beforeRequest: []}

Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially.

hooks.beforeRequest

Type: Function[]
Default: []

Called with the normalized request options. Got will make no further changes to the request before it is sent. This is especially useful in conjunction with got.extend() and got.create() when you want to create an API client that, for example, uses HMAC-signing.

.on('error', error, body, response)

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.

got.get(url, [options])

got.post(url, [options])

got.put(url, [options])

got.patch(url, [options])

got.head(url, [options])

got.delete(url, [options])

Sets options.method to the method name and makes a request.

Instances

got.extend([options])

Configure a new got instance with default options. options are merged with the parent instance's defaults.options using got.mergeOptions.

Cache

Got implements RFC 7234 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.

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.