documentation/migration-guides/request.md
You may think it's too hard to switch, but it's really not. 🦄
Let's take the very first example from Request's readme:
import request from 'request';
request('https://google.com', (error, response, body) => {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
});
With Got, it is:
import got from 'got';
try {
const response = await got('https://google.com');
console.log('statusCode:', response.statusCode);
console.log('body:', response.body);
} catch (error) {
console.log('error:', error);
}
Looks better now, huh? 😎
These Got options are the same as with Request:
urlbodyfollowRedirectencodingmaxRedirectslocalAddressheaderscreateConnectionhttp://unix:SOCKET:PATHThe time option does not exist, assume it's always true.
So if you're familiar with these, you're good to go.
Note:
Readability is very important to us, so we have different names for these options:
qs → searchParamsstrictSSL → rejectUnauthorizedgzip → decompressjar → cookieJar (accepts tough-cookie jar)jsonReviver → parseJsonjsonReplacer → stringifyJsonagent option is now an object with http, https and http2 properties.timeout option is now an object. You can set timeouts on particular events!searchParams option is always serialized using URLSearchParams.url option.got('https://example.com', {searchParams: {test: ''}}) → https://example.com/?test=got('https://example.com/?test') → https://example.com/?testgot.stream(url, options).json option is not a boolean, it's an object. It will be stringified and used as a body.form option is an object and will be used as application/x-www-form-urlencoded body.oauth / hawk / aws / httpSignature option.agentClass / agentOptions / pool option.forever option.keepAlive option set to true.proxy option. You need to pass a custom agent.auth option.username / password instead or set the authorization header manually.baseUrl option.prefixUrl which appends a trailing slash if not present.removeRefererHeader option.referer header in a beforeRequest hook.followAllRedirects option.copyPipedHeaders defaults to false.copyPipedHeaders: true for proxy scenarios.copyPipedHeaders: true, explicitly set headers win over piped headers.Hooks are very powerful. Read more to see what else you achieve using hooks.
Let's take a quick look at another example from Request's readme:
http.createServer((serverRequest, serverResponse) => {
if (serverRequest.url === '/doodle.png') {
serverRequest.pipe(request('https://example.com/doodle.png')).pipe(serverResponse);
}
});
Request can proxy headers with the stream. Got can do that too, but it is opt-in:
import {pipeline as streamPipeline} from 'node:stream/promises';
import got from 'got';
const server = http.createServer(async (serverRequest, serverResponse) => {
if (serverRequest.url === '/doodle.png') {
await streamPipeline(
serverRequest,
got.stream('https://example.com/doodle.png', {copyPipedHeaders: true}),
serverResponse
);
}
});
server.listen(8080);
In terms of stream usage, nothing has really changed, but header proxying is opt-in via copyPipedHeaders: true.
request.get, request.post, and so on - you can do the same with Got.request.defaults({…}) method has been renamed. You can do the same with got.extend({…}).request.cookie() nor request.jar(). You have to use tough-cookie directly.Well, you have already come this far :tada:
Take a look at the documentation. It's worth the time to read it.
There are some great tips.
If something is unclear or doesn't work as it should, don't hesitate to open an issue.