|
| 1 | +# Migration guides |
| 2 | + |
| 3 | +> :star: Switching from other HTTP request libraries to Got :star: |
| 4 | +
|
| 5 | +### Migrating from Request |
| 6 | + |
| 7 | +You may think it's too hard to switch, but it's really not. 🦄 |
| 8 | + |
| 9 | +Let's take the very first example from Request's readme: |
| 10 | + |
| 11 | +```js |
| 12 | +const request = require('request'); |
| 13 | + |
| 14 | +request('https://google.com', (error, response, body) => { |
| 15 | + console.log('error:', error); // Print the error if one occurred |
| 16 | + console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received |
| 17 | + console.log('body:', body); // Print the HTML for the Google homepage |
| 18 | +}); |
| 19 | +``` |
| 20 | + |
| 21 | +With Got, it is: |
| 22 | + |
| 23 | +```js |
| 24 | +const got = require('got'); |
| 25 | + |
| 26 | +(async () => { |
| 27 | + try { |
| 28 | + const response = await got('https://google.com'); |
| 29 | + console.log('statusCode:', response.statusCode); |
| 30 | + console.log('body:', response.body); |
| 31 | + } catch (error) { |
| 32 | + console.log('error:', error); |
| 33 | + } |
| 34 | +})(); |
| 35 | +``` |
| 36 | + |
| 37 | +Looks better now, huh? 😎 |
| 38 | + |
| 39 | +#### Common options |
| 40 | + |
| 41 | +Both Request and Got accept [`http.request` options](https://nodejs.org/api/http.html#http_http_request_options_callback). |
| 42 | + |
| 43 | +These Got options are the same as with Request: |
| 44 | + |
| 45 | +- [`url`](https://github.com/sindresorhus/got#url) (+ we accept [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) instances too!) |
| 46 | +- [`body`](https://github.com/sindresorhus/got#body) |
| 47 | +- [`json`](https://github.com/sindresorhus/got#json) |
| 48 | +- [`followRedirect`](https://github.com/sindresorhus/got#followRedirect) |
| 49 | +- [`encoding`](https://github.com/sindresorhus/got#encoding) |
| 50 | + |
| 51 | +So if you're familiar with them, you're good to go. |
| 52 | + |
| 53 | +Oh, and one more thing... There's no `time` option. Assume [it's always true](https://github.com/sindresorhus/got#timings). |
| 54 | + |
| 55 | +#### Renamed options |
| 56 | + |
| 57 | +Readability is very important to us, so we have different names for these options: |
| 58 | + |
| 59 | +- `qs` → [`query`](https://github.com/sindresorhus/got#query) |
| 60 | +- `strictSSL` → [`rejectUnauthorized`](https://github.com/sindresorhus/got#rejectUnauthorized) |
| 61 | +- `gzip` → [`decompress`](https://github.com/sindresorhus/got#decompress) |
| 62 | +- `jar` → [`cookieJar`](https://github.com/sindresorhus/got#cookiejar) (accepts [`tough-cookie`](https://github.com/salesforce/tough-cookie) jar) |
| 63 | + |
| 64 | +It's more clear, isn't it? |
| 65 | + |
| 66 | +#### Changes in behavior |
| 67 | + |
| 68 | +The [`timeout` option](https://github.com/sindresorhus/got#timeout) has some extra features. You can [set timeouts on particular events](readme.md#timeout)! |
| 69 | + |
| 70 | +The [`query` option](https://github.com/sindresorhus/got#query) is always serialized using [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) unless it's a `string`. |
| 71 | + |
| 72 | +The [`baseUrl` option](https://github.com/sindresorhus/got#baseurl) appends the ending slash if it's not present. |
| 73 | + |
| 74 | +There's no `maxRedirects` option. It's always set to `10`. |
| 75 | + |
| 76 | +To use streams, just call `got.stream(url, options)` or `got(url, {stream: true, ...}`). |
| 77 | + |
| 78 | +#### Breaking changes |
| 79 | + |
| 80 | +- No `form` option. You have to pass a [`form-data` instance](https://github.com/form-data/form-data) through the [`body` option](https://github.com/sindresorhus/got#body). |
| 81 | +- No `oauth`/`hawk`/`aws`/`httpSignature` option. To sign requests, you need to create a [custom instance](advanced-creation.md#signing-requests). |
| 82 | +- No `agentClass`/`agentOptions`/`pool` option. |
| 83 | +- No `forever` option. You need to use [forever-agent](https://github.com/request/forever-agent). |
| 84 | +- No `proxy` option. You need to [pass a custom agent](readme.md#proxies). |
| 85 | +- No `removeRefererHeader` option. You can remove the referer header in a [`beforeRequest` hook](https://github.com/sindresorhus/got#hooksbeforeRequest): |
| 86 | + |
| 87 | +```js |
| 88 | +const gotInstance = got.extend({ |
| 89 | + hooks: { |
| 90 | + beforeRequest: [ |
| 91 | + options => { |
| 92 | + delete options.headers.referer; |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +gotInstance(url, options); |
| 99 | +``` |
| 100 | + |
| 101 | +- No `jsonReviver`/`jsonReplacer` option, but you can use hooks for that too: |
| 102 | + |
| 103 | +```js |
| 104 | +const gotInstance = got.extend({ |
| 105 | + hooks: { |
| 106 | + beforeRequest: [ |
| 107 | + options => { |
| 108 | + if (options.json && options.jsonReplacer) { |
| 109 | + const newBody = {}; |
| 110 | + for (const [key, value] of Object.entries(options.body)) { |
| 111 | + let newValue = value; |
| 112 | + do { |
| 113 | + newValue = options.jsonReplacer(key, newValue); |
| 114 | + } while (typeof newValue === 'object'); |
| 115 | + |
| 116 | + newBody[key] = newValue; |
| 117 | + } |
| 118 | + |
| 119 | + // #1 solution (faster) |
| 120 | + options.body = newBody; |
| 121 | + |
| 122 | + // #2 solution (slower) |
| 123 | + options.body = JSON.parse(JSON.stringify(options.body, options.jsonReplacer)); |
| 124 | + } |
| 125 | + } |
| 126 | + ], |
| 127 | + afterResponse: [ |
| 128 | + response => { |
| 129 | + // TODO in Got: We need to make the `options` public somehow |
| 130 | + if (options.json && options.jsonReviver) { |
| 131 | + response.body = JSON.stringify(JSON.parse(response.body, options.jsonReviver)); |
| 132 | + } |
| 133 | + |
| 134 | + return response; |
| 135 | + } |
| 136 | + ] |
| 137 | + } |
| 138 | +}); |
| 139 | + |
| 140 | +gotInstance(url, options); |
| 141 | +``` |
| 142 | + |
| 143 | +Hooks are powerful, aren't they? [Read more](readme.md#hooks) to see what else you achieve using hooks. |
| 144 | + |
| 145 | +#### More about streams |
| 146 | + |
| 147 | +Let's take a quick look at another example from Request's readme: |
| 148 | + |
| 149 | +```js |
| 150 | +http.createServer((req, res) => { |
| 151 | + if (req.url === '/doodle.png') { |
| 152 | + req.pipe(request('https://example.com/doodle.png')).pipe(res); |
| 153 | + } |
| 154 | +}); |
| 155 | +``` |
| 156 | + |
| 157 | +The cool feature here is that Request can proxy headers with the stream, but Got can do that too: |
| 158 | + |
| 159 | +```js |
| 160 | +http.createServer((req, res) => { |
| 161 | + if (req.url === '/doodle.png') { |
| 162 | + req.pipe(got.stream('https://example.com/doodle.png')).pipe(res); |
| 163 | + } |
| 164 | +}); |
| 165 | +``` |
| 166 | + |
| 167 | +Nothing has really changed. Just remember to use `got.stream(url, options)` or `got(url, {stream: true, …`}). That's it! |
| 168 | + |
| 169 | +#### You're good to go! |
| 170 | + |
| 171 | +Well, you have already come this far. Take a look at the [documentation](readme.md#highlights). It's worth the time to read it. There are [some great tips](readme.md#aborting-the-request). If something is unclear or doesn't work as it should, don't hesitate to open an issue. |
0 commit comments