# Move from using request to alternative for HTTP

**URL:** <https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892>\
**Category:** Developing Nodes\
**Created:** [30 December 2021 15:55 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892 "2021-12-30T15:55:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [30 December 2021 15:55 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/1 "2021-12-30T15:55:46Z")

</div>

On the basis that `request` is deprecated, as part of looking at the fitbit node ([Fitbit nodes - working? - #4 by borpin](https://discourse.nodered.org/t/fitbit-nodes-working/55520/4)), I thought the first step might be to move the module away from `request` and to `node-fetch` (looking at the various docs, this seems the best alternative, but not wedded to that).

The current module uses `client-oauth2` and `request` to generate the HTTP requests.

Before I dive in and reinvent the wheel, does anyone have a readymade update to this that will work? Looking through the code, it seems this is the only point that `request` is actually used, however, I do appreciate that the overall structure might be inappropriate when not using `request` so may need a more fundamental rewrite (which is probably beyond my skills).

> <https://github.com/borpin/node-red-contrib-fitbit/blob/2b956f8eb82c0a470f4d4eb6384f7481f730d300/oauth-helper.js#L47-L60>

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [30 December 2021 16:22 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/2 "2021-12-30T16:22:18Z")

</div>

Hi @borpin,  
Don't know if you have noticed that the http-request node has moved some time ago from 'request' to [got](https://www.npmjs.com/package/got). Perhaps that can also be an alternative for you too? I haven't looked whether it supports oauth...  
Bart

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [30 December 2021 16:44 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/3 "2021-12-30T16:44:08Z")

</div>

Yes, that is my point; `request` has been deprecated and an alternative is required. `got` is one of the options to replace it.

I'm just looking to make the migration as painless as possible!

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [30 December 2021 18:01 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/4 "2021-12-30T18:01:03Z")

</div>

I've decided I hate `got` as it is impossible to search for examples and help. What an absurd name for a package!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [30 December 2021 21:28 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/5 "2021-12-30T21:28:39Z")

</div>

One of the reasons we chose it for core was that the API was most similar to request, but of course you can use whatever works for you.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 December 2021 21:40 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/6 "2021-12-30T21:40:42Z")

</div>

They have [quite extensive documentation](https://github.com/sindresorhus/got) ?

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [31 December 2021 08:07 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/7 "2021-12-31T08:07:00Z")

</div>

IS `got` therefore the preferred replacement? I'd not seen anything suggesting that directly?

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [31 December 2021 08:09 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/8 "2021-12-31T08:09:47Z")

</div>

Yes true, but when searching for examples and answers to questions is it one of the worst names possible. Why not `http-got`. Other names than are in the same positon was the UK firm **Thus** (didn't last long as a brand) and **JC** (which is really useful but too few letters to search on).

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2021 08:19 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/9 "2021-12-31T08:19:06Z")

</div>

I agree not the best name for searches. Luckily, Google is pretty good with context so you can add other key words like node, rest, http, endpoint etc

For example, try searching `node got post example` or `got get http`

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [31 December 2021 08:33 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/10 "2021-12-31T08:33:59Z")

</div>

For my specific purpose (linking with OAuth2) there is nothing that I can see, that helps.

For an experienced user the docs are OK (possibly), but for a relatively new starter, they are not sufficient.

Perhaps this is why the take-up of `got` is significantly lower than `node-fetch`.

> [@Steve-Mcl](#):
>
> Luckily, Google is pretty good with context so you can add other key words like node, rest, http, endpoint etc

Yes, it does help, but is still likely to not find a lot of examples :(.

Guessing no one has anything that can help my original question though.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2021 09:06 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/11 "2021-12-31T09:06:13Z")

</div>

> [@borpin](#):
>
> Guessing no one has anything that can help my original question though

Looking at the SRC you linked to in the OP, the node uses OAuth2. If you search the issues for OAuth2 on the `got` repository there are quite a few clues. For example this: [How to OAuth · Issue #219 · sindresorhus/got · GitHub](https://github.com/sindresorhus/got/issues/219#issuecomment-239612137)

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [31 December 2021 09:14 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/12 "2021-12-31T09:14:36Z")

</div>

Moving from `request` is not going to be done by me - simply do not have the knowledge.

The issue is that this Fitbit node currently used the `require` syntax to import and the `got` module requires the `import` syntax.

The 2 cannot coexist (it seems) so I'll have to stick with `request`.

> [@Steve-Mcl](#):
>
> If you search the issues for OAuth2 on the `got` repository

Yes I have now found that source of info - thanks. Again though, I'd suggest finding this under 'issues' is perhaps not the most user-friendly way 😉. [edit] Also, that post was 2016 and refers to Oauth1!

I'd also point out that the NPM page for `got` is basically blank ([got - npm](https://www.npmjs.com/package/got)). This doesn't help its cause.

I'll just have to move on and see if I can adapt what is there for the newer API syntax.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2021 09:24 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/13 "2021-12-31T09:24:53Z")

</div>

> [@borpin](#):
>
> I'd also point out that the NPM page for `got` is basically blank ([got - npm](https://www.npmjs.com/package/got)). This doesn't help its cause.

Looks quite extensive to me...

 ![Screenshot_20211231-091726](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/15c4ba591457d060192990db94c53f479e609369.jpeg)

> [@borpin](#):
>
> The issue is that this Fitbit node currently used the `require` syntax to import and the `got` module requires the `import` syntax

If you (or whoever) targets v11.x of `got` then you can require got just like node-red does: [node-red/packages/node\_modules/@node-red/nodes/core/network/21-httprequest.js at master · node-red/node-red · GitHub](https://github.com/node-red/node-red/blob/master/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js#L19)

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [31 December 2021 09:45 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/14 "2021-12-31T09:45:32Z")

</div>

Odd - I see this ☹

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/2134006e8cb055ea9fc822a610f16c9fe02fc60e.png)

> [@Steve-Mcl](#):
>
> If you (or whoever) targets v11.x of `got` then you can require got just like node-red does:

If I do that, I get the following;

```auto
Dec 31 09:35:55 nrtest Node-RED[1041]: 31 Dec 09:35:55 - [info] Node-RED version: v2.1.4
Dec 31 09:35:55 nrtest Node-RED[1041]: 31 Dec 09:35:55 - [info] Node.js version: v16.13.1
Dec 31 09:35:55 nrtest Node-RED[1041]: 31 Dec 09:35:55 - [info] Linux 5.4.78-2-pve x64 LE
Dec 31 09:35:58 nrtest Node-RED[1041]: 31 Dec 09:35:58 - [info] Loading palette nodes
Dec 31 09:35:58 nrtest Node-RED[1041]: 31 Dec 09:35:58 - [debug] Module: node-red-contrib-fitbit 1.0.9 /root/.node-red/node_modules/node-red-contrib-fitbit
Dec 31 09:36:04 nrtest Node-RED[1041]: 31 Dec 09:36:04 - [warn] ------------------------------------------------------
Dec 31 09:36:04 nrtest Node-RED[1041]: 31 Dec 09:36:04 - [warn] [node-red-contrib-fitbit/fitbit-credentials] Error [ERR_REQUIRE_ESM]: require() of ES Module /root/.node-red/node_modules/got/dist/source/index.js from /root/.node-red/node_modules/node-red-contrib-fitbit/oauth-helper.js not supported.
Dec 31 09:36:04 nrtest Node-RED[1041]: Instead change the require of index.js in /root/.node-red/node_modules/node-red-contrib-fitbit/oauth-helper.js to a dynamic import() which is available in all CommonJS modules. (line:2)
Dec 31 09:36:04 nrtest Node-RED[1041]: 31 Dec 09:36:04 - [warn] [node-red-contrib-fitbit/fitbit] Error [ERR_REQUIRE_ESM]: require() of ES Module /root/.node-red/node_modules/got/dist/source/index.js from /root/.node-red/node_modules/node-red-contrib-fitbit/oauth-helper.js not supported.
Dec 31 09:36:04 nrtest Node-RED[1041]: Instead change the require of index.js in /root/.node-red/node_modules/node-red-contrib-fitbit/oauth-helper.js to a dynamic import() which is available in all CommonJS modules. (line:52)

```

```auto
:~/.node-red# npm list got
node-red-project@0.0.1 /root/.node-red
`-- got@12.0.0

```

I'm definitely missing something ☹.

I note the example from core has the `require` under the `module.exports = function (RED)` statement. I have tried it there (and in other places to see if it would work).

The error message points to this in another file.

> <https://github.com/borpin/node-red-contrib-fitbit/blob/2b956f8eb82c0a470f4d4eb6384f7481f730d300/fitbit.js#L52-L52>

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2021 09:48 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/15 "2021-12-31T09:48:45Z")

</div>

You are targeting V12.

> [@Steve-Mcl](#):
>
> If you (or whoever) targets v11.x of `got` then you can require got

When you npm install got, use the same version as node-red. (or specify ^11.0.0 in package file of Fitbit node)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2021 09:51 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/16 "2021-12-31T09:51:47Z")

</div>

Actually, you might get away with a peer dependency of ^11 of got in your fork & when a user installs the node, it should (might) pick up the got version installed by node-red (unsure/untested)

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [31 December 2021 09:59 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/17 "2021-12-31T09:59:13Z")

</div>

Ah, OK. I'd read that as `^11.0.0` but you mean specifically V11.

> [@Steve-Mcl](#):
>
> When you npm install got, use the same version as node-red.

That bit lost me, I'm afraid. How can I tell which version Node-red uses? Can I just use that without installing it again?

Not keen on having to use a specific version of `got` as a workaround - seems worse than sticking with `request`.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 December 2021 10:05 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/18 "2021-12-31T10:05:13Z")

</div>

> [@borpin](#):
>
> That bit lost me, I'm afraid. How can I tell which version Node-red uses? Can I just use that without installing it again?

Look in the node-red package.json

> [@borpin](#):
>
> Not keen on having to use a specific version of `got` as a workaround - seems worse than sticking with `request` .

Yes, I agree.

So perhaps try the peer deps...

```auto
 "peerDependencies": {
    "got": "11.x"
  }

```

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [31 December 2021 10:05 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/19 "2021-12-31T10:05:47Z")

</div>

Got made the change to using ESM modules after 99.9% of the work to move to it from request in the http-request node had been done so we pinned it at v11.x rather than start again with another new library.

---

<div class="post-metadata">

**Author:** ![borpin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/borpin/32/1765_2.png) [@borpin](https://discourse.nodered.org/u/borpin)\
**Post date:** [31 December 2021 10:22 UTC](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892/20 "2021-12-31T10:22:13Z")

</div>

Thanks for all your help and patience.

For now, I'll stick with `request`.

[Next page](https://discourse.nodered.org/t/move-from-using-request-to-alternative-for-http/55892.md?page=2)
