Parsing XML (SOAP Response) through NetSuite Script - soap

In Netsuite Script I am trying to parse the response from EchoSign Webservice. The response from EchoSign is like this
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<getFormDataResponse xmlns="http://api.echosign">
<getFormDataResult xmlns="http://api.echosign">
<errorCode xmlns="http://dto8.api.echosign">OK</errorCode>
<errorMessage xmlns="http://dto8.api.echosign" xsi:nil="true" />
<formDataCsv xmlns="http://dto8.api.echosign">SOME DATA </formDataCsv>
<success xmlns="http://dto8.api.echosign">true</success>
</getFormDataResult>
</getFormDataResponse>
</soap:Body>
</soap:Envelope>
And my NetSuite code is here
var response = nlapiRequestURL(echoSignUrl, postStr, header);
var xml = nlapiStringToXML(response.getBody());
var resData = nlapiSelectNode(xml, 'soap:Envelope/soap:Body'); // /getFormDataResult/success');
if (resData)
return nlapiSelectValue(resData, 'formDataCsv');
But somehow I always get nothing back from nlapiSelectValue method!!

Nodes without any prefix should be accessed using default prefix nlapi:
for e.g. /soap:Envelope/soap:Body/nlapi:getFormDataResponse

It appears to be an issue with the way the namespacing is handled. Using the XML Tools plugin for Notepad++ and the XML you've provided, the XPATH Current Node Selector can't even find the getFormDataResponse node correctly. It simply fails with "Unknown Exception".
I also tried to manually evaluate the following expressions:
/soap:Envelope/soap:Body (works)
/soap:Envelope/soap:Body/getFormDataResponse (fails)
/soap:Envelope/soap:Body//getFormDataResponse (fails)
/soap:Envelope/soap:Body/*[local-name() = 'getFormDataResponse'] (fails)
/soap:Envelope/soap:Body/descendants::*[local-name() = 'getFormDataResponse'] (fails)
/soap:Envelope/soap:Body/descendants::getFormDataResponse (fails)
If I modify the XML so the EchoSign namespaces are in the Envelope with a prefix, like so:
<soap:Envelope xmlns:echo="http://api.echosign" xmlns:dto8="http://dto8.api.echosign" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<echo:getFormDataResponse>
<echo:getFormDataResult>
<dto8:errorCode>OK</dto8:errorCode>
<dto8:errorMessage xsi:nil="true" />
<dto8:formDataCsv>SOME DATA</dto8:formDataCsv>
<dto8:success>true</dto8:success>
</echo:getFormDataResult>
</echo:getFormDataResponse>
</soap:Body>
</soap:Envelope>
then the path selector can get all the way down to the formDataCsv node correctly. It returns the path: /soap:Envelope/soap:Body/echo:getFormDataResponse/echo:getFormDataResult/dto8:formDataCsv.
Not sure how helpful that is because you're not in control of the XML that EchoSign sends to you, but I'm not sure how to correctly format the XPATH to deal with the namespaces.

Related

javax.xml.soap.SOAPException: InputStream does not represent a valid SOAP 1.1 Message

I am consuming a SOAP API. The XML response I receive is surrounded with a 'soap envelope' - so I need to remove or parse past that wrapper before I can process the XML. I've taken the below approach with other endpoints (so the code is sane, at least) but with this particular endpoint, I get errors.
The error I'm encountering is:
SEVERE: SAAJ0304: InputStream does not represent a valid SOAP 1.1
Message
This is the code I am using to remove the Soap Wrapper:
String soapResponse = getSoapResponseFromApi();
ByteArrayInputStream inputStream = new ByteArrayInputStream(soapResponse.getBytes());
SOAPMessage message = MessageFactory.newInstance().createMessage(null, inputStream);
Document doc = message.getSOAPBody().extractContentAsDocument(); // <-- error thrown here
//unmarhsall the XML in 'doc' into an object
//do useful stuff with that object
here is the XML I am receiving back (the contents of soapResponse in the code above)
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<XMLContentIActuallyWant xmlns="http://my-url.com/webservices/">
<!-- Useful stuff here -->
</XMLContentIActuallyWant >
</soap:Body>
</soap:Envelope>
I discovered the solution as I was preparing this question.
Soap versions have different formatting. The SoapMessage library was defaulting to soap 1.1 - but the contents of the response I was receiving was soap 1.2.
I could see this when I inspected the full request that was being sent, in order to receive the response noted above - it looked like this:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<!-- xml content here -->
</soap12:Body>
</soap12:Envelope>
the soap12 part highlights that it is requesting soap 1.2.
so although the response doesn't contain the '12' - the response is also in 1.2.
So we need to tell SoapMessage to use 1.2 instead of the default (1.1 in my case).
I did this by modifying my code above like so:
Before:
SOAPMessage message = MessageFactory.newInstance().createMessage(null, inputStream);
After:
SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(null, inputStream);
It's worth noting that other endpoints of the same API served SOAP 1.1 - which is why this error was so confusing for me. I was doing the same thing and getting different results.

Get data from xml response in Jmeter

Hi Everyone!
I would like to get status from this response from Jmeter.How can i create regexp for that?
I just want to get 'vacant' from response
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns5:GetRandomMSISDNListResponse xmlns:ns2="http://www.trump.com/products/common/core" xmlns:ns3="http://www.trump.com/products/common/rr" xmlns:ns4="http://www.yota224.com/vux/domains/common/core" xmlns:ns5="http://www.yota224.com/vux/services/inventory">
<ns2:extension>
<ns2:item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:RequestContextType">
<ns2:correlationId>8f7ce7e1-31ae-40fe-82ba-4d59f634e599</ns2:correlationId>
</ns2:item>
</ns2:extension>
<msisdnList>
<homeRegion>1078</homeRegion>
<msisdn>9992146221</msisdn>
<status>vacant</status>
</msisdnList>
</ns5:GetRandomMSISDNListResponse>
</soap:Body>
</soap:Envelope>
If you targeting to do it via Regular Expressions - the relevant one would be something like:
<status>(\w+)</status>
Here is how it looks in the "RegExp Tester" mode of the View Results Tree listener
Also it might be easier to use XPath Extractor which is designed for working with XML data. In that case the relevant XPath query to get the status will be as simple as:
//status

Adwords SOAP XML request against ManagedCustomerService fails

I'm working with https://adwords.google.com/api/adwords/mcm/v201402/ManagedCustomerService and wanting to get the account hierarchy.
The requests are being made in raw XML (controlled by JScript) -- a bit perverse, I know, but that's the situation.
I've generated the following SOAP packet
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<ns1:RequestHeader soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:ns1="https://adwords.google.com/api/adwords/mcm/v201402">
<ns1:clientCustomerId>some_ccid</ns1:clientCustomerId>
<ns1:developerToken>some_developer_token</ns1:developerToken>
<ns1:userAgent>GAS</ns1:userAgent>
<ns1:validateOnly>false</ns1:validateOnly>
<ns1:partialFailure>false</ns1:partialFailure>
</ns1:RequestHeader>
</soapenv:Header>
<soapenv:Body>
<get xmlns="https://adwords.google.com/api/adwords/mcm/v201402">
<serviceSelector>
<fields>Login</fields>
<fields>Customer</fields>
<fields>Name</fields>
<predicate>
<field>id</field>
<operator>GREATER_THAN</operator>
<values>0</values>
</predicate>
</serviceSelector>
</get>
</soapenv:Body>
</soapenv:Envelope>
Please note the idGREATER_THAN0. This is my naive way of getting everything.
I notice that the PHP GetAccountHierarchy.php has
// Create selector.
$selector = new Selector();
// Specify the fields to retrieve.
$selector->fields = array('Login', 'CustomerId', 'Name');
// Make the get request.
$graph = $managedCustomerService->get($selector);
This would seem to imply that no predicate has been defined. However, I'm a bit leery of doing that because the documentation says (yes, I do read the friendly manual), "predicates ContentsNotNull"
The response I get is
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<ns2:ResponseHeader xmlns:ns2="https://adwords.google.com/api/adwords/mcm/v201402" xmlns="https://adwords.google.com/api/adwords/cm/v201402">
<requestId>0004f553e08eaca00abc25900000893f</requestId>
<serviceName>ManagedCustomerService</serviceName>
<methodName>get</methodName>
<operations>0</operations>
<responseTime>141</responseTime>
</ns2:ResponseHeader>
</soap:Header>
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>[QuotaCheckError.INVALID_TOKEN_HEADER # ]</faultstring>
<detail>
<ns2:ApiExceptionFault xmlns="https://adwords.google.com/api/adwords/cm/v201402" xmlns:ns2="https://adwords.google.com/api/adwords/mcm/v201402">
<message>[QuotaCheckError.INVALID_TOKEN_HEADER # ]</message>
<ApplicationException.Type>ApiException</ApplicationException.Type>
<errors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="QuotaCheckError">
<fieldPath/>
<trigger/>
<errorString>QuotaCheckError.INVALID_TOKEN_HEADER</errorString>
<ApiError.Type>QuotaCheckError</ApiError.Type>
<reason>INVALID_TOKEN_HEADER</reason>
</errors>
</ns2:ApiExceptionFault>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
What am I doing incorrectly that I should get such a response?
BTW, if I do leave out the predicate or specify it with a null content, I still get the QuotaCheckError.INVALID_TOKEN_HEADER error.
A solution has been provided on the Adwords API google group. Working nicely now!

Exchange EWS not returning message body for calendar

So i'm trying to fetch all the calendar event from office360.com . I am using ews to get the data. I sent a request of
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<soap:Body>
<FindItem xmlns="http://schemas.microsoft.com/exchange/services/2006/messages"
xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"
Traversal="Shallow">
<ItemShape>
<t:BaseShape>AllProperties</t:BaseShape>
<t:BodyType>HTML</t:BodyType>
</ItemShape>
<ParentFolderIds>
<t:DistinguishedFolderId Id="calendar"/>
</ParentFolderIds>
</FindItem>
</soap:Body>
</soap:Envelope>
This soap request xml is not returning me the calendar message body but i get the subject and other data . What am i doing wrong here ? any help is appreciated.
The body needs to be retrieved in a separate call.
http://weblogs.asp.net/psperanza/archive/2008/03/18/getting-calendar-items-using-exchange-web-services.aspx
Or you can load the property first.
http://blogs.msdn.com/b/exchangedev/archive/2010/03/16/loading-properties-for-multiple-items-with-one-call-to-exchange-web-services.aspx
The second is easier but you need to define all properties you want back and if it is a lot, it can be a pain.

LibXML doesn't find any nodes for my xpath expression

I'm using xpath and LibXML in an iPhone app to find some nodes in an xml document. I'm a noob in xpath so probably i am doing something wrong.
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetUserByEmailResponse xmlns="http://tempuri.org/">
<GetUserByEmailResult>
<id>4006</id>
<name>Kudor Gyozo</name>
<email/>
<image/>
<facebookId>0</facebookId>
</GetUserByEmailResult>
</GetUserByEmailResponse>
</soap:Body>
</soap:Envelope>
The xpath expression is //GetUserByEmailResult. No nodes are returned. I expect to get <GetUserByEmailResult> back. If I test the same stuff here it works ok.
UPDATE1: This is what i get back from a .net web service. Is there a way to ignore namespaces in libxml?
I'm not familiar with the iphone library, but you'll probably have to declare the namsepace / prefix somewhere: If you register 'http://tempuri.org/' with 'someprefix', you can search for:
//someprefix:GetUserByEmailResult
A possible workaround is (but not advisable):
//*[local-name()='GetUserByEmailResult']
The GetUserByEmailResult element is in the http://tempuri.org/ namespace so you would need to add that namespace to your XPath search.
You can do so using the xmlXPathRegisterNs function:
xmlXPathRegisterNs(context, BAD_CAST "temp", BAD_CAST "http://tempuri.org/");
Then your XPath would have to be changed to
//temp:GetUserByEmailResult