javascriptnode.jsrestpostget

Steps to send a https request to a rest service in Node js


What are the steps to send a https request in node js to a rest service? I have an api exposed like (Original link not working...)

How to pass the request and what are the options I need to give for this API like host, port, path and method?


Solution

  • The builtin http and https modules are perfectly good if a bit low-level. You'll probably want to use a module that handles the details for you and provides a nice higher-level API.

    In node v18, node itself exposes a global fetch(url) method, although as of 2023 it is still considered experimental and subject to change. Also, perhaps surprisingly, node's builtin fetch() global does not use the HTTP stack provided by the traditional builtin http/https modules. Instead, it uses a parallel, from-scratch HTTP stack rewrite called undici.

    The builtin fetch() is convenient, but given the instability and that it's still missing a few features, I'd avoid it in production use until the feature is more stable.

    There are several popular modules available on npm.