I have a problem with soap-node, my header have auth:
XML-service SOAP
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<Autentication xmlns="http://tempuri.org/">
<Pass>string</Pass>
<Us>string</Us>
</Autentication>
</soap:Header>
<soap:Body>
<getdata xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
App.js - Node.js
var soap = require('soap');
var url = "http://localhost/abc/example.asmx?WSDL"
var Autentication = {
"Autentication": {
"Pass" : "david",
"Us" : "hackro"
}
}
var args = {}
soap.createClient(url, function(err, client) {
if(err)return console.log(err)
client.addSoapHeader(Autenticacion)
client.ObtieneKey(args, function(err, result,body) {
if(err) return console.log(body)
console.log(result);
console.log(body);
})
})
But my auth fail because return empty
{ data: "" }
I see this answer but it doesn't work.
I solved my problem sending my header in this way
var Autentication ='<Autentication xmlns="http://tempuri.org/">Pass>david</Pass><Us>hackro</Us>/Autentication></soap:Header>'
Add header
client.addSoapHeader(Autentication)
Good Luck
Related
I would like to send an API request to https://api-test.onlineszamla.nav.gov.hu/invoiceService/queryTaxpayer and they expect an XML, as a sample provided below. What is the appropriate syntax in Node.js that processes this request? Unfortuantely I couldn't find this out from the API documentation.
EDIT: part of the XML contents will be from user data, part of it from input fields.
<?xml version="1.0" encoding="UTF-8"?><QueryTaxpayerRequest xmlns="http://schemas.nav.gov.hu/OSA/1.0/api">
<header>
<requestId></requestId>
<timestamp></timestamp>
<requestVersion></requestVersion>
<headerVersion></headerVersion>
</header>
<user>
<login></login>
<passwordHash></passwordHash>
<taxNumber></taxNumber>
<requestSignature></requestSignature>
</user>
<software>
<softwareId></softwareId>
</software>
<taxNumber></taxNumber>
</QueryTaxpayerRequest>
Use XML parser and then Set the response type as XML like below :
var xml = require('xml');
response.set('Content-Type', 'text/xml');
response.send(xml(YOUR_XML_OBJECT));
in the above I have used XML as the parser, you should be able to use any other available packages.
You can use a library like xmlbuilder to make creating xml string easier.
var builder = require('xmlbuilder');
var obj = {
root: {
xmlbuilder: {
repo: {
'#type': 'git', // attributes start with #
'#text': 'git://github.com/oozcitak/xmlbuilder-js.git' // text node
}
}
}
};
var xml = builder.create(obj).end({ pretty: true});
console.log(xml);
And then you can use any library to make the request and use the xml string above in the body.
This works for me awesome.
var Request = require("request");
const express = require("express");
const app = express();
app.use(express.urlencoded({extended: true}));
app.use(express.text())
app.post("/api/getXML", (req, res) => {
Request.post({
"headers": { "content-type": "text/plain; charset=utf-8"},
"url": "<url which return xml response>",
"body": req.body
}, (error, response, body) => {
if(error) {
console.error(error);
res.send(error)
}
console.log("XML body :",body);
res.send(body);
});
});
The idea got from the link https://www.thepolyglotdeveloper.com/2017/10/consume-remote-api-data-nodejs-application/
I am using the following Node Js code to use the Web service. But when I run this on Postman, I got [object Object] in console. But when I run on SoapUI, it will print the Session details
my code
router.post("/register", upload.single('image'), function (req, res, next)
{
var url = 'http://smeapps.mobitel.lk:8585/EnterpriseSMSV2/EnterpriseSMSWS?wsdl';
var session = {username: 'username', password: 'password'}
soap.createClient(url, function(err, client)
{
if(err)
console.log(err)
client.createSession(session, function(err, result)
{
console.log(result);
});
})
})
SoapUI session request
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.esms.mobitel.lk/">
<soapenv:Header/>
<soapenv:Body>
<ws:createSession>
<!--Optional:-->
<arg0>
<!--Optional:-->
<customer>12</customer>
<!--Optional:-->
<id>12</id>
<!--Optional:-->
<password>password</password>
<!--Optional:-->
<username>username</username>
</arg0>
</ws:createSession>
</soapenv:Body>
</soapenv:Envelope>
SoapUI response
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.esms.mobitel.lk/">
<SOAP-ENV:Body>
<ns1:createSessionResponse>
<return>
<expiryDate>2018-10-04T06:24:25+05:30</expiryDate>
<isActive>true</isActive>
<sessionId>761033662786</sessionId>
<user>0</user>
</return>
</ns1:createSessionResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
wrap the code into try catch
Error: SOAP-ENV:Server: Procedure 'createSession' not present
{"statusCode":500,"body":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>Procedure 'createSession' not present</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>\n","headers":{"date":"Thu, 04 Oct 2018 08:51:04 GMT","server":"Apache/2.2.15
(CentOS)","x-powered-by":"PHP/5.3.3","content-length":"304","connection":"close","content-type":"text/xml; charset=utf-8"},"request":{"uri":{"protocol":"http:","slashes":true,"auth":null,"host":"202.129.232.190:8585","port":"8585","hostname":"202.129.232.190","hash":null,"search":null,"query":null,"pathname":"/EnterpriseSMSV2/EnterpriseSMSWS.php","path":"/EnterpriseSMSV2/EnterpriseSMSWS.php","href":"http://202.129.232.190:8585/EnterpriseSMSV2/EnterpriseSMSWS.php"},"method":"POST","headers":{"User-Agent":"node-soap/0.8.0","Accept":"text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8","Accept-Encoding":"none","Accept-Charset":"utf-8","Connection":"close","Host":"202.129.232.190:8585","Content-Length":0,"Content-Type":"text/xml; charset=utf-8","SOAPAction":"\"\""}}}
Seems you are requesting REST API.
Response format for SOAP UI is XML. But for REST API is JSON. Then your are getting JSON format response. Thats why you are getting [object object] when you try to print that. So check out how to handle JSON.
I am using strong-soap (https://www.npmjs.com/package/strong-soap) for consuming wsdl from Node JS
I have a wsdl with header like below:-
<soapenv:Header>
<wsse:Security xmlns:wsse="http://xyz.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-24" xmlns:wsu="http://secure.xsd">
<wsse:Username>userid</wsse:Username>
<wsse:Password Type="http://pwdtext">password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
I need to add this header information while creating client.
I tried like
var url = "test?wsdl";
soap.createClient(url, {wsdl_headers: {"Username": "username","Password":"password"} }, function(err, client) {
//some logic
});
But every time I was getting soap fault "Authentication Failed".
Any idea what I am doing wrong?
Thanks in advance.
I had the same issue trying to pull data.
my mistake was that in the options of the createClient method is was using headers instead of wsdl_headers
also set the same authentication on the client before any method is called
my code looks as
var url = 'https://datahere?wsdl';
var httpOptions = {
wsdl_headers: {
'Authorization': 'Basic ' + new Buffer('username' + ':' + 'password').toString('base64')
}
};
soap.createClient(url, httpOptions, function(err, client) {
if (err) {
console.log(err.message);
response.status(401).end();
} else {
var requestArgs = {
Method1: 'dummyData',
Method2: ''
};
// client.setSecurity(new soap.BasicAuthSecurity('password', 'password'));
client.addHttpHeader('customHeader1', 'words');
client.addHttpHeader('Authorization', "Basic " + new Buffer('username-app-maker' + ':' + 'password').toString('base64'));
client.GETSOAPMETHOD(requestArgs, function(err, result) {
if (err) {
console.log(err.message);
}
console.log('i found ' + result);
response.send(result);
});
}
});
As mentioned in this answer, wsdl_header object is expecting a key 'Authentication'
So try running the following code:
var url = 'test?wsdl';
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");
soap.createClient(url, { wsdl_headers: {Authorization: auth} }, function(err, client) {
});
I am trying to upload a jrxml file to Jasper server through putRequest api. A report entry is added in server MyReports path using following xml input
xml
<?xml version="1.0" encoding="UTF-8"?>
<request operationName="put" locale="en">
<argument name="CREATE_REPORTUNIT_BOOLEAN">true</argument>
<!--MainReportunit-->
<resourceDescriptor name="TestReport1" wsType="reportUnit" uriString="/reports/Myreports/TestReport1" isNew="true">
<label><![CDATA[TestReport1]]></label>
<description><![CDATA[TestReport1]]></description>
<resourceProperty name="PROP_RESOURCE_TYPE">
<value><![CDATA[com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit]]></value>
</resourceProperty>
<resourceProperty name="PROP_PARENT_FOLDER">
<value><![CDATA[/reports/Myreports]]></value>
</resourceProperty>
<resourceProperty name="PROP_RU_ALWAYS_PROPMT_CONTROLS">
<value><![CDATA[true]]></value>
</resourceProperty>
<resourceProperty name="PROP_RU_CONTROLS_LAYOUT">
<value><![CDATA[1]]></value>
</resourceProperty>
<resourceDescriptor wsType="datasource" isNew="false">
<resourceProperty name="PROP_REFERENCE_URI">
<value><![CDATA[/datasources/{DATASOURCE_NAME}]]></value>
</resourceProperty>
<resourceProperty name="PROP_IS_REFERENCE">
<value><![CDATA[true]]></value>
</resourceProperty>
</resourceDescriptor>
<resourceDescriptor name="TestReport1" wsType="jrxml" uriString="/reports/Myreports/TestReport1" isNew="true">
<label><![CDATA[TestReport1]]></label>
<description><![CDATA[TestReport1]]></description>
<resourceProperty name="PROP_RESOURCE_TYPE">
<value><![CDATA[com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource]]></value>
</resourceProperty>
<resourceProperty name="PROP_HAS_DATA">
<value><![CDATA[true]]></value>
</resourceProperty>
<resourceProperty name="PROP_RU_IS_MAIN_REPORT">
<value><![CDATA[true]]></value>
</resourceProperty>
</resourceDescriptor>
</resourceDescriptor>
</request>
node js code
var parameters = {
putRequest: RequestXmlStr
}
var soap = require('soap');
soap.createClient(url, function (err, client) {
client.put(parameters, function (err, result) {
if (err) console.error(err);
});
});
It is working fine without attachment. But I dont know, where to include the jrxml data or base64 string in this xml.
Since SOAP is not a requirement, here is how I got it working with the REST_v2 service:
var request = require("request"),
fs = require("fs"),
reportUnitDescriptor = {
"label": "My Report Unit",
"description": "My First Report Unit",
"alwaysPromptControls": "true",
"controlsLayout": "popupScreen",
"dataSource": {
"dataSourceReference": {
"uri": "/analysis/datasources/FoodmartDataSourceJNDI"
}
},
"jrxml": {
"jrxmlFile": {
"label": "Main JRXML",
"type": "jrxml"
}
}
};
request.post({
url: "http://localhost:8080/jasperserver/rest_v2/resources/public",
auth: { user: "jasperadmin", password: "jasperadmin" },
formData: {
resource: {
value: JSON.stringify(reportUnitDescriptor),
options: {
contentType: "application/repository.reportUnit+json"
}
},
jrxml: fs.createReadStream(__dirname + '/MainReport.jrxml'),
}
}, function(err, response, body) {
console.log("status: " + response.statusCode + "; message: " + response.statusMessage);
});
Some notes:
The reportUnit will be created in the Public folder. You specify that in the URL after the /resources part.
The MainReport.jrxml is located in the same directory as this script is
More info on the REST_v2 API here
How to generate soap request in node.js for the below xml
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cred="http://credential.transport.v1.api.cmpublish.clickability.com" xmlns:tran="http://transport.v1.api.cmpublish.clickability.com">
<soapenv:Header>
<cred:credentials username="" password="" customerID=""/>
</soapenv:Header>
<soapenv:Body>
<tran:getSubscriberByEmail>
<email></email>
<domainID></domainID>
</tran:getSubscriberByEmail>
</soapenv:Body>
</soapenv:Envelope>
If you've got a WSDL to point things at, I've found node-soap to be straight forward.
From their readme:
var soap = require('soap');
var url = 'http://example.com/wsdl?wsdl';
var args = {name: 'value'};
soap.createClient(url, function(err, client) {
client.MyFunction(args, function(err, result) {
console.log(result);
});
});