SOLVED

How to read the responses from external API?

sachincs1991
Level 3
Level 3

Hi Team,

 

Below is the code 

 

var http = new HttpClientRequest("https://secure.mcommons.com/api/profile_update?phone_number=1234567890");
http.header["UserName"] = 'username';
http.header["Password"] = 'password';
http.header["Authorization"] = "Basic YXJjc3VwcG9ydEBzcGVyaWRpYW4uY29tOlNwZXJpZGlhbjIwMjE=";

http.header["Content-Type"] = "application/json";

http.method = "GET"
http.execute();

var r = http.response;
logInfo(r.body);

 

below is the response i get

 

<?xml version="1.0" encoding="UTF-8"?> <response success="true"> <message id="11787545911" type="generic" status="sent"> <phone_number>1234567890</phone_number> <profile>123456</profile> <body>Hello</body> <sent_at>2020-11-18 12:08:16 UTC</sent_at> <message_template_id/> <mms>false</mms> <multipart>false</multipart> <campaign id="12345" active="true"> <name> Testing</name> </campaign> </message> </response>

 

Here i need to read success, messageid, type, status, phone_number etc...

How Can we achieve it?

 

Thanks,

Sachin

1 Accepted Solution
Darren_Bowers
Correct answer by
Community Advisor
Community Advisor

Hi @sachincs1991 - you can parse the response as a DOMDocument and get the Elements and Attributes from there in the Javascript

e.g.

...
var doc = DOMDocument.fromXMLString(r.body);
var responseNode = doc.getElementsByTagName("response")[0];
var success = responseNode.getAttribute("success");
...

 Cheers

Darren

View solution in original post

11 Replies
Darren_Bowers
Correct answer by
Community Advisor
Community Advisor

Hi @sachincs1991 - you can parse the response as a DOMDocument and get the Elements and Attributes from there in the Javascript

e.g.

...
var doc = DOMDocument.fromXMLString(r.body);
var responseNode = doc.getElementsByTagName("response")[0];
var success = responseNode.getAttribute("success");
...

 Cheers

Darren

View solution in original post

sachincs1991
Level 3
Level 3

Hi @Darren_Bowers ,

 

Thank you very much for the reply.

 

I am able to read the success, but not the message id, status and others.

 

below is the code

 

var doc = DOMDocument.fromXMLString(resp.body);
var responseNode = doc.getElementsByTagName("response")[0];
var success = responseNode.getAttribute("success");
logInfo(success);
var responseNode1 = doc.getElementsByTagName("message")[0];
var id = responseNode1.getAttribute("id");
logInfo(id);

 

Error:

 

11/19/2020 12:27:21 AM SCR-160012 Javascript&colon; error while evaluating script 'DEVWKF5797/js6'. js6
11/19/2020 12:27:21 AM JST-310000 Error while compiling script 'DEVWKF5797/js6' line 26: responseNode1 is undefined. js6

Darren_Bowers
Community Advisor
Community Advisor
Strange - I copied your exact code and it works fine for me
sachincs1991
Level 3
Level 3

Hi,

 

Do we need to give any java class?

 

Thanks,

Sachin C S

Darren_Bowers
Community Advisor
Community Advisor
I would check your incoming message XML. If responseNode1 is saying its undefined that means the getElementByTagName("message")[0] probabaly returned a null result. To test I used your example XML in a string variable and the code worked fine. Cheers Darren
sachincs1991
Level 3
Level 3

Hi @Darren_Bowers,

 

Thank you very much for the reply.

 

Exactly right what you said 🙂

 

One more information required here we are sending the body of the message in the variable of the javascript itself.

Example as shown below.

var data = "Hello";
var campaignId = "123456";
var API = 'https://secure.mcommons.com/api/send_message?phone_number=';

 

Now we are trying to implement this approach through adding it in the SMS template body, is that possible.

If yes, How?

 

Kindly help us in this regards.

 

Thanks,

Sachin

 

Darren_Bowers
Community Advisor
Community Advisor
Hi Sachin - I would check your API documentation on how to achieve this. All messaging APIs are implemented differently so its hard to help you without having some API documentation. Cheers Darren
sachincs1991
Level 3
Level 3

Hi @Darren_Bowers 

 

In which schema do we store the responses which we receives from API?

 

Kindly find the below 2 API documentation for your reference.

One with POST and one with GET.

 

Thanks,

Sachin

 

 

rs GET API.PNGPOST API.PNG

 

 

Darren_Bowers
Community Advisor
Community Advisor
If you are calling a custom API then the responses are not stored in any schema. Cheers Darren
sachincs1991
Level 3
Level 3

Hi @Darren_Bowers ,

 

Hope you gone through the API responses which we get in my previous comment.

 

No, I have question like, we have tracking log, broad log and other logs for email where we track whether email sent failed, whether link in the email clicked or not.

 

Similarly when we send an SMS using external API, we want to track it?

How do we achieve it?

Expecting it to similarly implemented like email channel.

 

Thanks,

Sachin 

Darren_Bowers
Community Advisor
Community Advisor
Hi Sachin - I havent done this myself - maybe ask the question in another thread. I would assume a custom API would also require you to store your own data into the broadLog, trackingLogs, etc. using the Adobe JS APIs. Cheers Darren