Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

ERROR: httpResponse is not an object.

davidl14970702
Level 5
Level 5

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

1 Accepted Solution
Amit_Kumar
Correct answer by
Community Advisor
Community Advisor

ignore the second part, Forums formatting messed up. use this

  1. function getResponseJSON(request)
  2. {
  3. var responseJSON = {};
  4.   if(request && request.response && request.response.body) {
  5.     var rawResponse = request.response.body.toString();
  6.     if(rawResponse)
  7.     {
  8.       responseJSON = JSON.parse(rawResponse);
  9.       if(responseJSON)
  10.       {
  11.   logInfo(responseJSON.toSource());
  12.         return responseJSON;
  13.       }
  14.     }
  15.   }
  16.   
  17.   return responseJSON;
  18. }
  19. var url = "http://api.openweathermap.org/data/2.5/weather?q=mount%20juliet&APPID=a1fc052d2344c79bc4cb ecb9de90fc17&mode=json";
  20. var http = new HttpClientRequest(url);
  21. http.header["Content-Type"] = "application/json";
  22. http.method="Get";
  23. http.execute();
  24. var httpResponse =getResponseJSON(http);
  25. logInfo(httpResponse.name);

View solution in original post

0 Replies
Amit_Kumar
Community Advisor
Community Advisor

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

davidl14970702
Level 5
Level 5

Amit Kumar​,

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?

Amit_Kumar
Community Advisor
Community Advisor

yeah, it's &&. I made a typo.

here you go, do not forget to add required libraries.

  1. function getResponseJSON(request) 
  2. var responseJSON = {}; 
  3.   if(request && request.response && request.response.body) { 
  4.     var rawResponse = request.response.body.toString(); 
  5.     if(rawResponse) 
  6.     { 
  7.       responseJSON = JSON.parse(rawResponse); 
  8.       if(responseJSON) 
  9.       { 
  10.   logInfo(responseJSON.toSource()); 
  11.         return responseJSON; 
  12.       } 
  13.     } 
  14.   } 
  15.    
  16.   return responseJSON; 
  17. }
  18. var url = "http://api.openweathermap.org/data/2.5/weather?q=mount%20juliet&APPID=a1fc052d2344c79bc4cbecb9de90fc..."
  19. var http = new HttpClientRequest(url);
  20. http.header["Content-Type"] = "application/json";
  21. http.method="Get"; 
  22. http.execute();
  23. var httpResponse =getResponseJSON(http); 
  24. logInfo(httpResponse.name);
  1. function getResponseJSON(request) 
  2. var responseJSON = {}; 
  3.   if(request &&& request.response && request.response.body) { 
  4.     var rawResponse = request.response.body.toString(); 
  5.     if(rawResponse) 
  6.     { 
  7.       responseJSON = JSON.parse(rawResponse); 
  8.       if(responseJSON) 
  9.       { 
  10.   logInfo(responseJSON.toSource()); 
  11.         return responseJSON; 
  12.       } 
  13.     } 
  14.   } 
  15.    
  16.   return responseJSON; 
  17. }
Amit_Kumar
Correct answer by
Community Advisor
Community Advisor

ignore the second part, Forums formatting messed up. use this

  1. function getResponseJSON(request)
  2. {
  3. var responseJSON = {};
  4.   if(request && request.response && request.response.body) {
  5.     var rawResponse = request.response.body.toString();
  6.     if(rawResponse)
  7.     {
  8.       responseJSON = JSON.parse(rawResponse);
  9.       if(responseJSON)
  10.       {
  11.   logInfo(responseJSON.toSource());
  12.         return responseJSON;
  13.       }
  14.     }
  15.   }
  16.   
  17.   return responseJSON;
  18. }
  19. var url = "http://api.openweathermap.org/data/2.5/weather?q=mount%20juliet&APPID=a1fc052d2344c79bc4cb ecb9de90fc17&mode=json";
  20. var http = new HttpClientRequest(url);
  21. http.header["Content-Type"] = "application/json";
  22. http.method="Get";
  23. http.execute();
  24. var httpResponse =getResponseJSON(http);
  25. logInfo(httpResponse.name);

View solution in original post

davidl14970702
Level 5
Level 5

Amit Kumar

Thanks again!

I am now able to parse the object as expected!