Human-friendly and powerful HTTP request library for Node.js
Moving from Request? (Note that Request is unmaintained)
See how Got compares to other HTTP libraries
For browser usage, we recommend Ky by the same people.
Highlights
- Promise API
- Stream API
- Pagination API (experimental)
- Request cancelation
- RFC compliant caching
- Follows redirects
- Retries on failure
- Progress events
- Handles gzip/deflate/brotli
- Timeout handling
- Errors with metadata
- JSON mode
- WHATWG URL support
- Hooks
- Instances with custom defaults
- Types
- Composable
- Plugins
- Used by 3000+ packages and 1.6M+ repos
- Actively maintained
Install
$ npm install got
Usage
const got = require('got');
(async () => {
try {
const response = await got('https://sindresorhus.com');
console.log(response.body);
//=> '<!doctype html> ...'
} catch (error) {
console.log(error.response.body);
//=> 'Internal server error ...'
}
})();
Streams
const stream = require('stream');
const {promisify} = require('util');
const fs = require('fs');
const got = require('got');
const pipeline = promisify(stream.pipeline);
(async () => {
await pipeline(
got.stream('https://sindresorhus.com'),
fs.createWriteStream('index.html')
);
// For POST, PUT, and PATCH methods `got.stream` returns a `stream.Writable`
await pipeline(
fs.createReadStream('index.html'),
got.stream.post('https://sindresorhus.com')
);
})();
Tip: Using from.pipe(to)
doesn't forward errors. If you use it, switch to Stream.pipeline(from, ..., to, callback)
instead (available from Node v10).
API
It's a GET
request by default, but can be changed by using different methods or via options.method
.
By default, Got will retry on failure. To disable this option, set options.retry
to 0
.
got(url?, options?)
Returns a Promise for a response
object or a stream if options.isStream
is set to true.
url
Type: `string