Send and receive XML via Http Post Request

Posted by jsniemi79 on 04-Feb-2015 08:44

I'm trying to make a call out to a third party using http post.  I can get this to work using the Send Http Post Request trigger with a document template.  My issue is i need to get a response from the third party and store that information for use in another call.  it doesn't appear to be an option inside the trigger unless I'm missing something.  I tried to use the rbv_api.sendHttpPost API, but that appears to only work if I'm sending json and not xml.

Is this something that is possible?

All Replies

Posted by Gian Torralba on 04-Feb-2015 09:04

Hello,

Does the third party service provider have an option that will return the data as XML?

Thank you,
Gian

Posted by jsniemi79 on 04-Feb-2015 09:06

Yes, I get an xml response back from the Send Http Post Request trigger.  I'm just not sure how to do anything with the response.

Posted by Gian Torralba on 04-Feb-2015 09:56

Hello,

I believe you can use javascript code to read the XML response. Please see this forum post at http://stackoverflow.com/.../using-xmlhttprequest-reading-xml-data

Hope this helps.

Thank you,

Gian

Posted by jsniemi79 on 04-Feb-2015 10:20

Hi Gian,

Thanks for your response. I understand that i can parse through an xml response with javascript.  I came across a couple posts similar to the one you sent.  What I don't understand is where I would put that code.  When I call the Send Http Post Trigger on create, it sends the xml correctly to my third party.  That call has a response containing an ID that I need to use for a subsequent call.  I don't see anywhere on the trigger setup where I can tell Rollbase what to do with the response.  I assumed I couldn't put anything in the document template either since the whole thing is sent in the Http request.

Unless I am missing something, it appears the Send Http Post Request trigger is only useful when you are sending something to a third party and not expecting a response.

Posted by pvorobie on 04-Feb-2015 11:51

There is a way to process HTTP response in subsequent triggers through rbv_api.getSharedValue() API. However now we have much easier way: rbv_api.sendHttpPost() API in Object Script triggers. Use rbv_api.parseXML() to parse XML response.

Posted by jsniemi79 on 04-Feb-2015 12:50

I have tried the rbv_api.sentHttpPost and have not had any success in gettting a valid response from the third party.  It appears to be limited to the fact that the parameters I have to pass through this trigger must be in name/value pairs and their service is expecting pure xml.  If there is a trick to making that work, I would love to hear it.

Any other thoughts?

Posted by pvorobie on 04-Feb-2015 12:56

Please try

rbv_api.sendJSONRequest(url, data, method, contentType, username, password, headers)

Pass your XML as "data" parameter.

Posted by jsniemi79 on 04-Feb-2015 14:16

That worked to get my response.  Thank you very much for the help.

i'm trying to use the parse xml api and the results are not what I would have expected.  Here is the code I have showing what I'm debugging.

var resp = rbv_api.sendJSONRequest(url, data, method, contentType, null, null, null);

var root = rbv_api.parseXML(resp);

var nodes = root.getChildNodes();

   for (var k=0; k<nodes.length; k++) {

     var child = nodes.item(k);

     rbv_api.println("Tag="+child.getTagName());

     var nodes2 = child.getChildNodes();

     for (var n=0; n<nodes2.length; n++) {

     var child2 = nodes2.item(n);

     var x = child2.getNodeValue();

     rbv_api.println("Value="+x);

      }

    }

rbv_api.println('Root');

rbv_api.printArr(root);

rbv_api.println('\n Nodes');

rbv_api.printArr(nodes);

rbv_api.println('\n Child');

rbv_api.printArr(child);

rbv_api.println('\n Child2');

rbv_api.printArr(child2);

rbv_api.println('\n XML Response');

rbv_api.print(resp);

Here is what I get back from that code:

Tag=Response

Value=null

Value=null

Value=null

Value=null

Value=null

Value=null

Value=null

Value=null

Root

[TransactionSetupResponse: null]

Nodes

[TransactionSetupResponse: null]

Child

[Response: null]

Child2

[TransactionSetup: null]

XML Response

<TransactionSetupResponse xmlns='https://transaction.elementexpress.com'><Response><ExpressResponseCode>0</ExpressResponseCode><ExpressResponseMessage>Success</ExpressResponseMessage><ExpressTransactionDate>20150204</ExpressTransactionDate><ExpressTransactionTime>141230</ExpressTransactionTime><ExpressTransactionTimezone>UTC-06:00:00</ExpressTransactionTimezone><Transaction><TransactionSetupID>44F3709A-0655-4778-AAF2-449285612515</TransactionSetupID></Transaction><PaymentAccount><TransactionSetupID>44F3709A-0655-4778-AAF2-449285612515</TransactionSetupID></PaymentAccount><TransactionSetup><TransactionSetupID>44F3709A-0655-4778-AAF2-449285612515</TransactionSetupID><ValidationCode>BC4862ED41BE4C81</ValidationCode></TransactionSetup></Response></TransactionSetupResponse>

All I need to get is the TransactionSetupID from the response.

Thanks again for your help on this.

Posted by pvorobie on 04-Feb-2015 14:24

If you only want to extract a single value from XML document, try using XPATH API:

rbv_api.evalXpath(xmlString, xpathExpr)

Traversing DOM structure may be tedious exercise.

Posted by jsniemi79 on 04-Feb-2015 14:44
Posted by pvorobie on 04-Feb-2015 15:16

Try this:

var myXpath = "/TransactionSetupResponse/Response/Transaction/TransactionSetupID/text()";

var res = rbv_api.evalXpath(resp, myXpath);

for (var k=0; k<res.length; k++)

rbv_api.println("Result: "+res.item(k));

var transactionSetupID = res.item(0).getNodeValue();

rbv_api.println("transactionSetupID="+transactionSetupID);

Posted by pvorobie on 04-Feb-2015 15:17
Posted by jsniemi79 on 04-Feb-2015 15:19

That worked perfectly!  Thank you very much for your help.

This thread is closed