50

GitHub - sindresorhus/got: Simplified HTTP requests

 5 years ago
source link: https://github.com/sindresorhus/got
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

readme.md



Got


Huge thanks to 68747470733a2f2f73696e647265736f726875732e636f6d2f6173736574732f7468616e6b732f6d6f78792d6c6f676f2e737667 for sponsoring me!



Simplified HTTP requests

Build Status: Linux Build status: Windows Coverage Status Downloads Install size

Got is a human-friendly and powerful HTTP request library.

It was created because the popular request package is bloated: Install size

Highlights

Install

$ npm install got
68747470733a2f2f63352e70617472656f6e2e636f6d2f65787465726e616c2f6c6f676f2f6265636f6d655f615f706174726f6e5f627574746f6e4032782e706e67

Usage

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
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 with a body property, an url property with the request URL or the final URL after redirects, and a requestUrl property with the original request URL.

The response object will typically be a Node.js HTTP response stream, however, if returned from the cache it will be a response-like object which behaves in the same way.

The response will also have a fromCache property set with a boolean value.

url

Type: string Object

The URL to request, as a string, a https.request options object, or a WHATWG 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 options.

baseUrl

Type: string Object

When specified, url will be prepended by baseUrl.
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.

headers

Type: Object
Default: {}

Request headers.

Existing headers will be overwritten. Headers set to null will be omitted.

stream

Type: boolean
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

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

encoding

Type: string null
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.

body must be a plain object. It will be converted to a query string using (new URLSearchParams(object)).toString().

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: string Object

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

timeout

Type: number Object

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.
  • secureConnect starts when connect completes and ends when the handshaking process completes (HTTPS only).
  • socket starts when the socket is connected. See request.setTimeout.
  • response starts when the request has been written to the socket and ends when the response headers are received.
  • send starts when the socket is connected and ends with the request has been written to the socket.
  • request starts when the request is initiated and ends when the response's end event fires.
retry

Type: number Object
Default:

  • retries: 2
  • methods: GET PUT HEAD DELETE OPTIONS TRACE
  • statusCodes: 408 413 429 502 503 504
  • maxRetryAfter: undefined

An object representing retries, methods, statusCodes and maxRetryAfter fields for the time until retry, allowed methods, allowed status codes and maximum Retry-After time.

If maxRetryAfter is set to undefined, it will use options.timeout.
If Retry-After header is greater than maxRetryAfter, it will cancel the request.

Delays between retries counts with function 1000 * Math.pow(2, retry) + Math.random() * 100, where retry is attempt number (starts from 0).

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).

Note: It retries only on the specified methods, status codes, and on these network errors:

  • ETIMEDOUT: One of the timeout limits were reached.
  • ECONNRESET: Connection was forcibly closed by a peer.
  • EADDRINUSE: Could not bind to any free port.
  • ECONNREFUSED: Connection was refused by the server.
  • EPIPE: The remote side of the stream being written has been closed.
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

Type: Object
Default: false

Cache adapter instance for storing cached data.

useElectronNet

Type: boolean
Default: false

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.

See the AWS section for an example.

Note: Modifying the body is not recommended because the content-length header has already been computed and assigned.

Streams

Note: Progress events, redirect events and request/response events can also be used with promises.

got.stream(url, [options])

Sets options.stream to true.

Returna a duplex stream with additional events:

.on('request', request)

request event to get the request object of the request.

Tip: You can use request event to abort request:

got.stream('github.com')
	.on('request', request => setTimeout(() => request.abort(), 50));
.on('response', response)

The response event to get the response object of the final request.

.on('redirect', response, nextOptions)

The redirect event to get the response object of a redirect. The second argument is options for the next request to the redirect location.

.on('uploadProgress', progress)
.on('downloadProgress', progress)

Progress events for uploading (sending a request) and downloading (receiving a response). The progress argument is an object like:

{
	percent: 0.1,
	transferred: 1024,
	total: 10240
}

If it's not possible to retrieve the body size (can happen when streaming), total will be null.

(async () => {
	const response = await got('sindresorhus.com')
		.on('downloadProgress', progress => {
			// Report download progress
		})
		.on('uploadProgress', progress => {
			// Report upload progress
		});

	console.log(response);
})();
.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.

const client = got.extend({
	baseUrl: 'https://example.com',
	headers: {
		'x-unicorn': 'rainbow'
	}
});

client.get('/demo');

/* HTTP Request =>
 * GET /demo HTTP/1.1
 * Host: example.com
 * x-unicorn: rainbow
 */
(async () => {
	const client = got.extend({
		baseUrl: 'httpbin.org',
		headers: {
			'x-foo': 'bar'
		}
	});
	const {headers} = (await client.get('/headers', {json: true})).body;
	//=> headers['x-foo'] === 'bar'

	const jsonClient = client.extend({
		json: true,
		headers: {
			'x-baz': 'qux'
		}
	});
	const {headers: headers2} = (await jsonClient.get('/headers')).body;
	//=> headers2['x-foo'] === 'bar'
	//=> headers2['x-baz'] === 'qux'
})();

Need more control over the behavior of Got? Check out the got.create().

got.mergeOptions(parentOptions, newOptions)

Extends parent options. Avoid using object spread as it doesn't work recursively:

const a = {headers: {cat: 'meow', wolf: ['bark', 'wrrr']}};
const b = {headers: {cow: 'moo', wolf: ['auuu']}};

{...a, ...b}            // => {headers: {cow: 'moo', wolf: ['auuu']}}
got.mergeOptions(a, b)  // => {headers: {cat: 'meow', cow: 'moo', wolf: ['auuu']}}

Options are deeply merged to a new object. The value of each key is determined as follows:

  • If the new property is set to undefined, it keeps the old one.
  • 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).
  • If the new property is a plain Object:
    • If the parent property is a plain Object too, both values are merged recursively into a new Object.
    • Otherwise, only the new value is deeply cloned.
  • If the new property is an Array, it overwrites the old one with a deep clone of the new property.
  • Otherwise, the new value is assigned to the key.

Errors

Each error contains (if available) statusCode, statusMessage, host, hostname, method, path, protocol and url properties to make debugging easier.

In Promise mode, the response is attached to the error.

got.CacheError

When a cache method fails, for example, if the database goes down or there's a filesystem error.

got.RequestError

When a request fails. Contains a code property with error class code, like ECONNREFUSED.

got.ReadError

When reading from response stream fails.

got.ParseError

When json option is enabled, server response code is 2xx, and JSON.parse fails.

got.HTTPError

When the server response code is not 2xx. Includes statusCode, statusMessage, and redirectUrls properties.

got.MaxRedirectsError

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.

got.UnsupportedProtocolError

When given an unsupported protocol.

got.CancelError

When the request is aborted with .cancel().

got.TimeoutError

When the request is aborted due to a timeout

Aborting the request

The promise returned by Got has a .cancel() method which when called, aborts the request.

(async () => {
	const request = got(url, options);

	//

	// In another part of the code
	if (something) {
		request.cancel();
	}

	//

	try {
		await request;
	} catch (error) {
		if (request.isCanceled) { // Or `error instanceof got.CancelError`
			// Handle cancelation
		}

		// Handle other errors
	}
})();

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.

You can use the JavaScript Map type as an in-memory cache:

const got = require('got');
const map = new Map();

(async () => {
		let response = await got('sindresorhus.com', {cache: map});
		console.log(response.fromCache);
		//=> false

		response = await got('sindresorhus.com', {cache: map});
		console.log(response.fromCache);
		//=> true
})();

Got uses Keyv internally to support a wide range of storage adapters. For something more scalable you could use an official Keyv storage adapter:

$ npm install @keyv/redis
const got = require('got');
const KeyvRedis = require('@keyv/redis');

const redis = new KeyvRedis('redis://user:pass@localhost:6379');

got('sindresorhus.com', {cache: redis});

Got supports anything that follows the Map API, so it's easy to write your own storage adapter or use a third-party solution.

For example, the following are all valid storage adapters:

const storageAdapter = new Map();
// Or
const storageAdapter = require('./my-storage-adapter');
// Or
const QuickLRU = require('quick-lru');
const storageAdapter = new QuickLRU({maxSize: 1000});

got('sindresorhus.com', {cache: storageAdapter});

View the Keyv docs for more information on how to use storage adapters.

Proxies

You can use the tunnel module with the agent option to work with proxies:

const got = require('got');
const tunnel = require('tunnel-agent');

got('sindresorhus.com', {
	agent: tunnel.httpOverHttp({
		proxy: {
			host: 'localhost'
		}
	})
});

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.

const got = require('got');
const HttpAgent = require('agentkeepalive');
const HttpsAgent = HttpAgent.HttpsAgent;

got('sindresorhus.com', {
	agent: {
		http: new HttpAgent(),
		https: new HttpsAgent()
	}
});

Cookies

You can use the cookie module to include cookies in a request:

const got = require('got');
const cookie = require('cookie');

got('google.com', {
	headers: {
		cookie: cookie.serialize('foo', 'bar')
	}
});

got('google.com', {
	headers: {
		cookie: [
			cookie.serialize('foo', 'bar'),
			cookie.serialize('fizz', 'buzz')
		].join(';')
	}
});

Form data

You can use the form-data module to create POST request with form data:

const fs = require('fs');
const got = require('got');
const FormData = require('form-data');
const form = new FormData();

form.append('my_file', fs.createReadStream('/foo/bar.jpg'));

got.post('google.com', {
	body: form
});

OAuth

You can use the oauth-1.0a module to create a signed OAuth request:

const got = require('got');
const crypto  = require('crypto');
const OAuth = require('oauth-1.0a');

const oauth = OAuth({
	consumer: {
		key: process.env.CONSUMER_KEY,
		secret: process.env.CONSUMER_SECRET
	},
	signature_method: 'HMAC-SHA1',
	hash_function: (baseString, key) => crypto.createHmac('sha1', key).update(baseString).digest('base64')
});

const token = {
	key: process.env.ACCESS_TOKEN,
	secret: process.env.ACCESS_TOKEN_SECRET
};

const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';

got(url, {
	headers: oauth.toHeader(oauth.authorize({url, method: 'GET'}, token)),
	json: true
});

Unix Domain Sockets

Requests can also be sent via unix domain sockets. Use the following URL scheme: PROTOCOL://unix:SOCKET:PATH.

  • PROTOCOL - http or https (optional)
  • SOCKET - Absolute path to a unix domain socket, for example: /var/run/docker.sock
  • PATH - Request path, for example: /v2/keys
got('http://unix:/var/run/docker.sock:/containers/json');

// Or without protocol (HTTP by default)
got('unix:/var/run/docker.sock:/containers/json');

AWS

Requests to AWS services need to have their headers signed. This can be accomplished by using the aws4 package. This is an example for querying an "API Gateway" with a signed request.

const AWS = require('aws-sdk');
const aws4 = require('aws4');
const got = require('got');

const credentials = await new AWS.CredentialProviderChain().resolvePromise();

// Create a Got instance to use relative paths and signed requests
const awsClient = got.extend(
	{
		baseUrl: 'https://<api-id>.execute-api.<api-region>.amazonaws.com/<stage>/',
		hooks: {
			beforeRequest: [
				async options => {
					await credentials.getPromise();
					aws4.sign(options, credentials);
				}
			]
		}
	}
);

const response = await awsClient('endpoint/path', {
	// Request-specific options
});

Testing

You can test your requests by using the nock module to mock an endpoint:

const got = require('got');
const nock = require('nock');

nock('https://sindresorhus.com')
	.get('/')
	.reply(200, 'Hello world!');

(async () => {
	const response = await got('sindresorhus.com');
	console.log(response.body);
	//=> 'Hello world!'
})();

If you need real integration tests you can use create-test-server:

const got = require('got');
const createTestServer = require('create-test-server');

(async () => {
	const server = await createTestServer();
	server.get('/', 'Hello world!');

	const response = await got(server.url);
	console.log(response.body);
	//=> 'Hello world!'

	await server.close();
})();

Tips

User Agent

It's a good idea to set the 'user-agent' header so the provider can more easily see how their resource is used. By default, it's the URL to this repo. You can omit this header by setting it to null.

const got = require('got');
const pkg = require('./package.json');

got('sindresorhus.com', {
	headers: {
		'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
	}
});

got('sindresorhus.com', {
	headers: {
		'user-agent': null
	}
});

304 Responses

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.

Custom endpoints

Use got.extend() to make it nicer to work with REST APIs. Especially if you use the baseUrl option.

Note: Not to be confused with got.create(), which has no defaults.

const got = require('got');
const pkg = require('./package.json');

const custom = got.extend({
	baseUrl: 'example.com',
	json: true,
	headers: {
		'user-agent': `my-module/${pkg.version} (https://github.com/username/my-module)`
	}
});

// Use `custom` exactly how you use `got`
(async () => {
	const list = await custom('/v1/users/list');
})();

Related

  • gh-got - Got convenience wrapper to interact with the GitHub API
  • gl-got - Got convenience wrapper to interact with the GitLab API
  • travis-got - Got convenience wrapper to interact with the Travis API
  • graphql-got - Got convenience wrapper to interact with GraphQL
  • GotQL - Got convenience wrapper to interact with GraphQL using JSON-parsed queries instead of strings

Maintainers

Sindre Sorhus Vsevolod Strukchinsky Alexander Tesfamichael Luke Childs Szymon Marczak Brandon Smith Sindre Sorhus Vsevolod Strukchinsky Alexander Tesfamichael Luke Childs Szymon Marczak Brandon Smith

License

MIT


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK