SOAP call with node.js

Post Reply
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

SOAP call with node.js

Post by billy.olvestad »

Is there any examples of how to do a SOAP call with node.js in Switch?
I can't find any information about how you do this with node.js, only the old SOAP class for the old scripting environment.
I am completely new to node.js, but thought I should start learning with this specific task.
laurentd
Member
Posts: 137
Joined: Wed Mar 13, 2019 2:06 pm

Re: SOAP call with node.js

Post by laurentd »

The easiest option will be to use one of the npm packages for this.
https://www.npmjs.com/search?q=soap
Laurent De Wilde, Solution Architect @ Enfocus
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: SOAP call with node.js

Post by billy.olvestad »

laurentd wrote: Tue Feb 22, 2022 12:05 pm The easiest option will be to use one of the npm packages for this.
https://www.npmjs.com/search?q=soap
Ok. Thanks, I will have a look at it.
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: SOAP call with node.js

Post by billy.olvestad »

I am stuck on an error in Switch when running my script. I am a beginner with node.js, so be kind to me :D
The code is as follows (I have replaced server names and authorization with nonsense, of course)

Code: Select all

async function jobArrived(s, flowElement, job) {
	
	const soapRequest = require('easy-soap-request');
	const fs = require("fs-extra");
	
	const jobPath = await job.get(AccessLevel.ReadOnly);
	const url = 'http://url_to_soap_server';
	const Headers = {
		'Accept-Encoding': 'gzip,deflate',
		'Content-Type': 'text/xml;charset=UTF-8',
		'SOAPAction': 'soap-action',
		'Authorization': 'Basic bXlVc2VyOm15UGFzc3dvcmQK',
		'Content-Length': '1643',
		'Host': 'host-address',
		'Connection': 'Keep-Alive',
		'User-Agent': 'Apache-HttpClient/4.5.5 (Java/16.0.1)',
	};
	
	const xml = await fs.readFile(jobPath, { encoding: "utf8" });
	await job.log(LogLevel.Info, xml, 'xmltext');
	
	(async () => {
	  
	  await job.log(LogLevel.Info, 'one'); //Just to see how far we get
	  
	  const { response } = await soapRequest({ url: url, headers: Headers, xml: xml, timeout: 3000 });
	  
	  await job.log(LogLevel.Info, 'two'); //Just to see how far we get
	  
	  const { headers, body, statusCode } = response;
	  
	  await job.log(LogLevel.Info, 'three'); //Just to see how far we get
	  
	  await job.setPrivateData('headers: ', headers);
	  await job.setPrivateData('body: ', body);
	  await job.setPrivateData('statusCode: ', statusCode);
	})();
	
	

	
	const jobname = await job.getName(true);
	
	await job.sendToSingle(jobname);
	
}
When I run this script, it gets to the line where i write "one" to the switch log, and then it stops. The warning I get in the switch log is as follows.
The script execution has ended while an async function or callback in the script might not have finished yet. Please check the script code for unresolved promises or missing await statements.
I can't figure out what I have done wrong. Can anyone catch my mistake?
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: SOAP call with node.js

Post by billy.olvestad »

It seems like something goes wrong at line 25, and the node.js step locks up, but the job goes on in the flow. I can't stop the flow before i have force quit the node.js service.
Up until that line everything executes properly.

There is an await statement on that line, so I don´t know why it doesn´t wait.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: SOAP call with node.js

Post by Padawan »

You have a self executing function halfway your script. That doesn't look right, can you try this?
(Loading modules is almost always done outside your jobArrived, so I also fixed that)

Code: Select all

const soapRequest = require('easy-soap-request');
const fs = require("fs-extra");

async function jobArrived(s, flowElement, job) {
	const jobPath = await job.get(AccessLevel.ReadOnly);
	const url = 'http://url_to_soap_server';
	const Headers = {
		'Accept-Encoding': 'gzip,deflate',
		'Content-Type': 'text/xml;charset=UTF-8',
		'SOAPAction': 'soap-action',
		'Authorization': 'Basic bXlVc2VyOm15UGFzc3dvcmQK',
		'Content-Length': '1643',
		'Host': 'host-address',
		'Connection': 'Keep-Alive',
		'User-Agent': 'Apache-HttpClient/4.5.5 (Java/16.0.1)',
	};
	
	const xml = await fs.readFile(jobPath, { encoding: "utf8" });
	await job.log(LogLevel.Info, xml, 'xmltext');
	  
	  await job.log(LogLevel.Info, 'one'); //Just to see how far we get
	  
	  const { response } = await soapRequest({ url: url, headers: Headers, xml: xml, timeout: 3000 });
	  
	  await job.log(LogLevel.Info, 'two'); //Just to see how far we get
	  
	  const { headers, body, statusCode } = response;
	  
	  await job.log(LogLevel.Info, 'three'); //Just to see how far we get
	  
	  await job.setPrivateData('headers: ', headers);
	  await job.setPrivateData('body: ', body);
	  await job.setPrivateData('statusCode: ', statusCode);

	const jobname = await job.getName(true);
	
	await job.sendToSingle(jobname);
	
}
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: SOAP call with node.js

Post by billy.olvestad »

Ok, I made the changes, and now it is reporting another error:
Actions with jobs can not be performed inside a 'jobArrived' entry point
It still executes the log of the word "one" to the switch log, so up until then it seems to execute
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: SOAP call with node.js

Post by billy.olvestad »

I have tried to solve this problem, but no matter what I do the execution stops at this line

Code: Select all

const { response } = await soapRequest({ url: url, headers: Headers, xml: xml, timeout: 3000 });
The error, or rather warning I get (yellow colour in the messages window) is:
Actions with jobs can not be performed inside a 'jobArrived' entry point
And nothing else executes after that.
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: SOAP call with node.js

Post by billy.olvestad »

I gave up.

I tried two different soap client libraries for node.js, but both of them had some sort of problem that prevented them from getting a response from this specific soap server, and other libraries were just too complicated to use for me.

I found that you can do a soap request with powershell instead, and created a batch file that calls powershell and makes the request.
That finally worked and solved my problem.
Post Reply