Source code: source/as-promise/index.ts
The main Got function returns a Promise.
Request aborting is supported via the signal option and AbortController.
got(url: string | URL, options?: OptionsInit, defaults?: Options)
Returns: Promise<Response>
The most common way is to pass the URL as the first argument, then the options as the second.
import got from 'got';
const {headers} = await got(
'https://httpbin.org/anything',
{
headers: {
foo: 'bar'
}
}
).json();Returns: Promise<T>
A shortcut method that gives a Promise returning a JSON object.
It is semantically the same as setting options.resolveBodyOnly to true and options.responseType to 'json'.
Returns: Promise<Uint8Array>
A shortcut method that gives a Promise returning a Uint8Array.
It is semantically the same as setting options.resolveBodyOnly to true and options.responseType to 'buffer'.
Returns: Promise<string>
A shortcut method that gives a Promise returning a string.
It is semantically the same as setting options.resolveBodyOnly to true and options.responseType to 'text'.
The events are the same as in Stream API.
Registers a one-time listener for events from Stream API.
Removes listener registered with promise.on.
import {createReadStream} from 'node:fs';
import got from 'got';
const ongoingRequestPromise = got.post(uploadUrl, {
body: createReadStream('sample.txt')
});
const eventListener = (progress: Progress) => {
console.log(progress);
};
ongoingRequestPromise.on('uploadProgress', eventListener);
setTimeout(() => {
ongoingRequestPromise.off('uploadProgress', eventListener);
}, 500);
await ongoingRequestPromise;