DEV Community

Stanislav Berkov
Stanislav Berkov

Posted on • Edited on

2

node-fetch request having custom certificate

UPDATE 2/24/2025
As of Nodejs v23.8 you can use system certificates, see
https://nodejs.org/en/blog/release/v23.8.0#support-for-using-system-ca-certificates-store-on-macos-and-windows


While developing you have to deal with internal servers that use enterprise SSL certificate. Nodejs has its own certificate storage. Thereby you need to provide certificates explicitly.

const fetch = require("node-fetch").default;
const https = require("https");
const fs = require("fs");

const ca = fs.readFileSync("./mozilla-ca-plus-custom.pem").toString();
const agent = new https.Agent({ ca });

async function main() {
    const res = await fetch("https://server.local/master/api2/v1/UserPages", {
        "headers": {
            "accept": "application/json, text/plain, */*",
            "accept-language": "en-US,en;q=0.9,ru;q=0.8",
            "authorization": "Bearer 1eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3cGZlX3VzZXJfaWQiOiIzZTQwNjZkMi04Mjg2LTQyNzktOWFjNC04YzNlNTNhMjNjMjYiLCJ3cGZlX2xvZ2luIjoic2JlcmtvdiIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6WyJNYXN0ZXJBZG1pbiIsIkNvbnRlbnRFZGl0b3IiLCJUcmFuc2xhdG9yIiwiTW9uaXRvciJdLCJzdWIiOiI0YTBiZGQyMC01YzJkLTI1NGUtYTE2ZC03YjY1MDVlZjI1NTgiLCJuYmYiOjE3MDE0MDk3NjgsImV4cCI6MTczMjk0NTc2OCwiaXNzIjoiV3BmZVNlcnZlciIsImF1ZCI6IldwZmVTZXJ2ZXIgQ2xpZW50cyJ9.k_aaDTeJUyxYv--KBVJYZCBSAHiG_eGHACz2dqCvr5w",
            "content-type": "application/json",
            "sec-ch-ua": "\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "\"Windows\"",
            "sec-fetch-dest": "empty",
            "sec-fetch-mode": "cors",
            "sec-fetch-site": "same-origin",
        },
        "body": "{}"
        "method": "PUT",
        agent
    });

    const text = await res.text();
    console.log(text);
}

main();
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay