Nikolay Siderov
Nikolay Siderov

Reputation: 1

Attach .pfx certificate to nodejs/axios HTTP request

I am doing a bank integration host-to-host with dais-software.com.

The proxy, the XML payload, the URL and SOAP Action are working correctly.

The issue is with attaching the .PFX Certificate , when I try it with postman it works, but in nodejs - it doesn't.


import * as https from 'node:https';
import fs from 'fs';
const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');

// Proxy configuration
const proxyUrl = new URL('proxy-url');
const proxyOptions = {
    host: proxyUrl.hostname,
    port: proxyUrl.port,
    auth: proxyUrl.username && proxyUrl.password ? `${proxyUrl.username}:${proxyUrl.password}` : undefined,
};

// Create the HTTPS agent with the .pfx certificate
const agent = new https.Agent({
    pfx: fs.readFileSync('./cert.pfx'),
    passphrase: 'pass',
    host: 'https://website.com:443',
    rejectUnauthorized: false,
});

const axiosAgent = new HttpsProxyAgent({
    ...proxyOptions,
    httpsAgent: agent,
});

// XML payload
const xmlRequest = `xml content`;

// Headers
const headers = {...};

// Send POST request
axios
    .post('https://website.com/SessionService.svc', xmlRequest, {
        headers,
        httpsAgent: axiosAgent,
    })
    .then((response) => {
        console.log('Response:', response.data);
    })

The reponse from the server is about the cert: "m_safeCertContext is an invalid handle."

I have tried with all Chat GPT suggestions but without any result. The .pfx certificate is found and loaded, the passphrase is correct, the host is correct.

Upvotes: 0

Views: 22

Answers (0)

Related Questions