Alternatives to request-promise-native [closed]
Asked Answered
C

4

13

I was looking for modern modules that implement basic HTTP methods such as GET, POST in Node.js.

I guess the most popular is request. The async/await version of it is called request-promise-native.

Recently I learned that these modules are being deprecated. So, what modern alternatives can I use that are built on the async/await paradigm?

Cuthbert answered 21/2, 2020 at 8:14 Comment(6)
Did you follow the link to alternatives in that issue?Peck
I found only bentCuthbert
There's a whole list linked from that issue thread. But also: why don't you use bent, then?Peck
It looks like bent has not gained popularity (7000 downloads weekly). I want to choose something I can get help for.Cuthbert
Then #60335048Peck
See also github.com/sindresorhus/got#comparisonBallyrag
R
8

I'd strongly suggest using node-fetch. It is based on the fetch API in modern browsers. Not only is it promise-based it also has an actual standard behind it.

The only reason you wouldn't use fetch is if you don't like the API. Then I'd suggest using something cross-platform like axios or superagent.

I personally find using the same API on the server and browser eases maintainability and offers potential for code reuse.

Ringster answered 21/2, 2020 at 8:50 Comment(0)
L
5

Just for having another option in mind, I would suggest using the node native http module.

import * as http from 'http';

async function requestPromise(path: string) {
    return new Promise((resolve, reject) => {
        http.get(path, (resp) => {
            let data = '';

            resp.on('data', (chunk) => {
                data += chunk;
            });

            resp.on('end', () => {
                resolve(data);
            });

        }).on("error", (error) => {
            reject(error);
        });
    });
}

(async function () {
    try {
        const result = await requestPromise('http://www.google.com');
        console.log(result);
    } catch (error) {
        console.error(error);
    }
})();

Lelalelah answered 21/2, 2020 at 8:43 Comment(0)
K
2

On the same github issue for request there is another link which talks about the alternatives. You can see them here. It clearly explains different types and wht style they are (promise/callback).

Kentledge answered 21/2, 2020 at 8:42 Comment(0)
B
1

Got is pretty sweet.

"Got was created because the popular request package is bloated. Furthermore, Got is fully written in TypeScript and actively maintained." - https://www.npmjs.com/package/got#faq

enter image description here

Ballyrag answered 9/12, 2021 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.