How to read the responses from external API? | Community
Skip to main content
Level 3
November 19, 2020
Solved

How to read the responses from external API?

  • November 19, 2020
  • 1 reply
  • 3145 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Darren_Bowers

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

1 reply

Darren_Bowers
Darren_BowersAccepted solution
Level 9
November 19, 2020

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

Level 3
November 23, 2020

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