I am using the HTTPRequest to GET lead lists form a third party (FB,MailChimp ect..). I can make the correct request and get back data but I am not sure of what steps to take next. I thought I could just use dot notion to iterate through the nested objects/array's. How can I iterate through the response object for specific keys?
Adobe CampaignAdobe CampaignAPI Error
Here is my code:
var url = "http://api.openweathermap.org/data/2.5/weather?q=mount%20juliet&APPID=a1fc052d2344c79bc4cbecb9de90fc...";
var http = new HttpClientRequest(url);
http.header["Content-Type"] = "application/xml";
http.method="Get";
http.execute();
var httpResponse = http.response.body.toString();
logInfo(httpResponse);
What comes next? Why does httpResponse.name not return the city name of the object?
If I just take the toString() result (literally copy and paste) it into another var I am able to get the key:value pair.
var wd = {"coord": {"lon":-86.52,"lat":36.2},
"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],
"base":"stations",
"main":{"temp":305.65,"pressure":1018,"humidity":31,"temp_min":304.15,"temp_max":306.15},
"visibility":16093,
"wind":{"speed":1.5,"deg":220},
"clouds":{"all":1},"dt":1528494960,
"sys":{"type":1,"id":2522,"message":0.0038,"country":"US","sunrise":1528453704,"sunset":1528506150},
"id":4643336,
"name":"Mount Juliet",
"cod":200};
//this loop returns all the keys in the object
Object.keys(wd).forEach(function(key){
//logInfo(key);
});
var name = wd.name;
logInfo(name);
--Correct LOG--
06/10/2018 8:58:41 AM Mount Juliet
When I use the method toDocument() I get the 3 keys available, but I still can't index or iterate to the correct key:value pair.
var httpResponse = http.response.body.toDocument();
Object.keys(httpResponse).forEach(function(key){
logInfo(key);
});
06/10/2018 8:59:08 PM root
06/10/2018 8:59:08 PM documentURI
06/10/2018 8:59:08 PM documentElement
This gets me 'name is undefined'
var httpResponse = http.response.body.toString();
var name = http.response.body.name;
logInfo(name);
It doesn't seem like Adobe Campaign uses dot or bracket notation to parse the httpResponse objects. What am I missing?
Message was edited by: David Loyd
Solved! Go to Solution.
ignore the second part, Forums formatting messed up. use this
HI David,
Use below function to get the response from Request obj
function getResponseJSON(request)
{
var responseJSON = {};
if(request &&& request.response && request.response.body) {
var rawResponse = request.response.body.toString();
if(rawResponse)
{
responseJSON = JSON.parse(rawResponse);
if(responseJSON)
{
logInfo(responseJSON.toSource());
return responseJSON;
}
}
}
return responseJSON;
}
Regards,
Amit
thank you for the timely response.
I get a syntax error on the &&&. is that meant to be &&?
Perhaps I am a bit too novice, but could you provide some explanation on how to use this? I've plugged this function in a few different ways and I'm not returning anything (besides the syntax error). no errors and no data.
Is the 'request' variable in your function supposed to be my key:value pair I am looking for?
Views
Replies
Total Likes
yeah, it's &&. I made a typo.
here you go, do not forget to add required libraries.
Views
Replies
Total Likes
ignore the second part, Forums formatting messed up. use this
Views
Replies
Total Likes