Unable to use Buffer function

I am trying to create a script to login into a SOAP API service and am running an issue with the Buffer function.

The example that were provided to me use the following header for the https.request command:

headers: {
“Content-Type”: “text/xml; charset=utf-8”,
“Content-Length”: Buffer.byteLength(SOAPbodyVariable),
Authorization: "Basic " + new Buffer(username + “:” + password).toString(“base64”),
SOAPAction: “http://webserviceURL”,
Accept: “application/json”
}

This basically stops my script from working. If i comment out or delete:
“Content-Length”: Buffer.byteLength(SOAPbodyVariable),
Authorization: "Basic " + new Buffer(username + “:” + password).toString(“base64”),
the script works again.
I am not sure if i am missing somehting, but it seems that i am hung on anything that has to do with “Buffer”.

Generally my attempt to create a soap call is:

function APIcall(SOAPbodyVariable) {
var cred = getcreds();
var url = ‘https://webservicesurl’;
var options = {
host: url,
port: 443,
method: “POST”,
path: cred.asmxpath,
headers: {
“Content-Type”: “text/xml; charset=utf-8”,
“Content-Length”: Buffer.byteLength(SOAPbodyVariable),
Authorization: "Basic " + " new Buffer(cred.username + “:” + cred.password).toString(“base64”),
SOAPAction: “http:/soapURL/action”,
Accept: “application/json”
}

};

request = https.request(options, function (res) {
console.log(“statusCode:”, res.statusCode);
res.on(“data”, (d) => { log process.stdout.write(d); });
});
request.on(“error”, (e) => { console.error(e); });
request.end(xml);

return request;

}

Because i can not get the Buffer issue resolved i don’t even know if the actual request actually works and whether i am returning the results properly.

Thank you for any assistance
CE

Hi @ceichermueller! Welcome to the Glitch forums! Are you setting up those headers on the client side? If so, that might be the issue – I think the Buffer object is specific to Node.js. If your project is public, you can share the project name and people can take a look!

The header is defined in the same js file in a different function.
the file starts out like this:

function credentials() {...}    
    //where the credentials and asmx/wsdl paths are stored
function buildSOAPenvelope(soapbody){...}    
    //where the body is injected in the soap:envelope so different actions can bde built
function APIcall(completeSOAPpackage) {...}         
   //this is where i am having the issue with the buffer 

soapbody = "<soap:Body>....";
var completeSOAPpackage = buildSOAPenvelope(soapbody);
var CallResult = APIcall(completeSOAPpackage);
var returninfo = {     //this is whats returned to my HTML page.
    soapcall: completeSOAPpackage,
    apiresult: CallResult,
    xmlsoapbody: soapbody,
    };
return returninfo;
    //reaturn each step to HTML page

HTML output with “buffer not included”:

Soap Body:
    <soap:Body><create xmlns="http://soapdomain/WS/><Entities><Entity xsi:type="create" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Title>whatever</Title></Entity></Entities></create></soap:Body>

Soap Call:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:x-si="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header><API-Integration xmlns="http://soapdomain/WS/"> 
<IntegrationCode></IntegrationCode> </API-Integrations></soap:Header>

<soap:Body><create xmlns="http://soapdomain/ATWS/><Entities><Entity xsi:type="create" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Title>whatever</Title></Entity></Entities></create></soap:Body>
</soap:Envelope>

API result:
[object Object]

The output is currently for debugging so i can see that everything is put together properly.

What error message do you see, or what do you mean by not working?

Is this code running on a node server in glitch? As @househaunt was saying, Buffer isn’t native to browser javascript, and anyway the code shouldn’t be running on the browser because it is handling passwords.

FYI this header should really be

Accept: "text/xml; charset=utf-8"

to ensure you keep getting XML back, just in case the API server suddenly decides to support JSON and takes note of the Accept header, and the data changes format on you.

Authorization: "Basic " + " new Buffer(cred.username + ":" + cred.password).toString("base64"),
                          ^ the third double quote looks like a typo 

Also check for copy/paste errors such as double quotes converted to “fancy style” lol

Ok,. maybe its me not understanding how glitch is used. I am trying to use glitch to connect trello to a Soap API. So using information stored in trello (Ids, variables, usernames, pass, etc) i was going to connect to the SOAP API to query items in the remote system so i can return data back to trello. So I guess i am using glitch as a connector (recommended by trello for developing power ups). Is this even possible since i need to build the soap requests and execute the calls from glitch?
Am i approaching the process incorrectly?

thank you

Nothing wrong with the approach :slight_smile:

There are a bunch of SOAP modules on npm which aim to make it easier to do SOAP requests.