Epicor REST nodejs error EPROTO

I need to use the rest request for an epicor function that makes a new record in a UD table. It works in postman. When I use the nodejs https module it returns {"errno": -4046, "code": "EPROTO", "syscall": "write"}. I found the posts about npm epicor-rest-node, it seems like overkill for what I need to do, but if that’s the answer I’m happy to try it.

Here is my code stripped down to the relevant bits. Note the commented out lines where I’ve tried things with no change, or a noted unhelpful change:

const https = require('https');

async function epimail(o) {
    o = JSON.parse(o);
    let restkey = 'spicySecret';
    let restuser = 'JamesBond';
    let restpwd = 'shakenNotStirred';
    let ud15resturl = `https://mydomain.com/EpicorDev/api/v2/efx/GS/UD15RecsAdd/AddUD15Rec`;
    let emjso = {
        rectype: o.Key2,
        recid: o.Key3,
        bodyemail: o.GS_BodyEmail_c,
        emailto: o.GS_ToEmail_c,
        emailfrom: o.GS_FromEmail_c,
        issendready: o.GS_IsSendReady_c,
        userid: o.userid,
        subject: o.GS_SubjectLine_c,
        attachments: o.GS_Attachments_c,
        emailbcc: o.GS_BccEmail_c,
        emailcc: o.GS_CcEmail_c,
        creditviewonly: o.GS_CreditViewOnly_c,
        key4: o.Key4,
        key5: o.Key5
    }
    let hostpath = ud15resturl.replace('https://','').split('.com');
    let urlparams = {
        // protocol: 'https', // ERR_INVALID_PROTOCOL
        host: `${hostpath[0]}.com`,
        port: 80,
        path: hostpath[1],
        method: 'POST',
        //ciphers: 'DEFAULT:@SECLEVEL=0',
        //minVersion: "TLSv1.2",
        //maxVersion: "TLSv1.2",
        headers: {
            'Content-type': 'application/json',
            'x-api-key': restkey,
            'Authorization': `Basic ${btoa(restuser + ':' + restpwd)}`,
            'cache-control': 'no-cache'
        }
    }
    function SendRequest(senddata) {
        return new Promise((resolve, reject) => {
            function OnResponse(response) {
                var data = '';
                response.on('data', (chunk) => { data += chunk; });
                response.on('end', () => { resolve(data) });
            }
            let request = https.request(urlparams, OnResponse);
            request.on('error', (e) => { reject(e) });
            request.write(senddata);
            request.end();
        });
    }
    let newud15 = await SendRequest(JSON.stringify(emjso)).catch((e) => {
        let message = `Error using Epicor REST function: ${JSON.stringify(e)}`;
        return message;
    });
    return newud15;
}
module.exports.epimail = epimail;

Any help or clues to google would be very appreciated. I’ve heard good things about this forum.

I’ve done this from node, but might be a bit before I can find it.

Hopefully someone can come along and help before then.

1 Like

I did end up using npm epicor-rest-node.