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:
urlbodyfollowRedirectencodingmaxRedirectslocalAddressheaderscreateConnection- UNIX sockets:
http://unix:SOCKET:PATH
The time option does not exist, assume it's always true.
So if you're familiar with these, you're good to go.
Note:
- Got stores HTTPS options inside
httpsOptions. Some of them have been renamed. Read more.
Readability is very important to us, so we have different names for these options:
qs→searchParamsstrictSSL→rejectUnauthorizedgzip→decompressjar→cookieJar(acceptstough-cookiejar)jsonReviver→parseJsonjsonReplacer→stringifyJson
- The
agentoption is now an object withhttp,httpsandhttp2properties. - The
timeoutoption is now an object. You can set timeouts on particular events! - The
searchParamsoption is always serialized usingURLSearchParams. - In order to pass a custom query string, provide it with the
urloption.
got('https://example.com', {searchParams: {test: ''}})→https://example.com/?test=
got('https://example.com/?test')→https://example.com/?test - To use streams, call
got.stream(url, options)orgot(url, {…, isStream: true}).
- The
jsonoption is not aboolean, it's anobject. It will be stringified and used as a body. - The
formoption is anobjectand will be used asapplication/x-www-form-urlencodedbody. - All headers are converted to lowercase.
According to the spec, the headers are case-insensitive. - No
oauth/hawk/aws/httpSignatureoption.
To sign requests, you need to create a custom instance. - No
agentClass/agentOptions/pooloption. - No
foreveroption.
You need to pass an agent withkeepAliveoption set totrue. - No
proxyoption. You need to pass a custom agent. - No
authoption.
You need to useusername/passwordinstead or set theauthorizationheader manually. - No
baseUrloption.
Instead, there isprefixUrlwhich appends a trailing slash if not present. - No
removeRefererHeaderoption.
You can remove therefererheader in abeforeRequesthook. - No
followAllRedirectsoption.
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);
}
});The cool feature here is that Request can proxy headers with the stream, but Got can do that too!
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(
got.stream('https://example.com/doodle.png'),
serverResponse
);
}
});
server.listen(8080);In terms of streams nothing has really changed.
- If you were using
request.get,request.post, and so on - you can do the same with Got. - The
request.defaults({…})method has been renamed. You can do the same withgot.extend({…}). - There is no
request.cookie()norrequest.jar(). You have to usetough-cookiedirectly.
Well, you have already come this far 🎉
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.