I keep getting 'null' and 'undefined' reading JSON responseBody in RESTmessage script

christaylor
Kilo Contributor

I have a Web Service Rest Message set up that works fine. In a scheduled script, I have this code:

var r = new sn_ws.RESTMessageV2('My Test Web Service', 'get');

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

var parser = new global.JSON();    

var parsed = parser.decode(responseBody);

gs.log(parsed.entities[0].name);

I've tried a bunch of different things but I can never get it to print any of the elements of my decoded JSON string. I get this error:

org.mozilla.javascript.EcmaError: Cannot convert null to an object.

    Caused by error in <refname> at line 7

          4: var httpStatus = response.getStatusCode();

          5: var parser = new global.JSON();    

          6: var parsed = parser.decode(responseBody);

==>     7: gs.log(parsed.entities[0].name);

If I log "gs.log(parsed)" just to see what I get it says null.

I can't figure out what I'm doing wrong. I'm not even sure it's really sending a REST GET because I don't see anything in the ECC queue like I do if I run it from a workflow REST message activity. But, if I say "gs.log(response)" it logs   [object RESTResponseV2], so it looks like it's getting a response back. Here is what the response body is supposed to look like, tested using an API explorer on the web server:

{
  "metadata": {
  "grandTotalEntities": 2,
  "totalEntities": 2
  }
,
  "entities": [
  {
  "uuid": "dd0354e8-b691-470a-84b4-d78e64ff7e6a",
  "name": "CentOS-6.7-x86_64-bin-DVD1.iso",
  "annotation": "",
  "deleted": false,
  "containerId": 1072,
  "logicalTimestamp": 2,
  "imageType": "ISO_IMAGE",
  "vmDiskId": "4e1cd8c0-1f92-41cd-aa85-1d1e3d9cf8c3",
  "imageState": "ACTIVE",
  "createdTimeInUsecs": 1449270889358793,
  "updatedTimeInUsecs": 1449270889987846
  }
,
  {
  "uuid": "4c23affe-96ff-46e8-acc1-c6990ac3304b",
  "name": "CentOS-7-x86_64-DVD-1503-01.iso",
  "annotation": "",
  "deleted": false,
  "containerId": 1072,
  "logicalTimestamp": 3,
  "imageType": "ISO_IMAGE",
  "vmDiskId": "452e9fe8-a7bb-4ec0-abd4-86a71736c1a5",
  "imageState": "ACTIVE",
  "createdTimeInUsecs": 1450724128604482,
  "updatedTimeInUsecs": 1450735962991374
  }

  ]
}

9 REPLIES 9

Inacitve_User
Giga Contributor

Hi Chris,



I faced this problem earlier. i used for loop to wait for 60 seconds and print the response then you wont be seeing null.


I just say this method available out of box. 'waitForResponse(60)'. Check whether this works and if it doesn't work check using for loop.



Thanks,


Sujan.


christaylor
Kilo Contributor

I tried adding


response.waitForResponse(60);



But it logs the error about converting the null to object immediately. Why isn't it waiting 60 seconds?


Inacitve_User
Giga Contributor

i have used something like below. I have customized RestMessage but not RestMessageV2. Even i tried wait response during my development but didn't work and didn't dig further. So i have done some think like below. Hope this helps you.


var r = new sn_ws.RESTMessageV2('My Test Web Service', 'get');


var response = r.execute();


var k = 1;


while (response == null) {


  gs.print( "waiting ... " + k + " seconds");


  response = r.getResponse(1000);


  k++;



  if ( k > 30 ) {


  gs.print( 'service time-out' );


  break;


  }


}


Hi Sujan Reddy ,



Try this test code. Modify according to your need



var r = new sn_ws.RESTMessageV2('My test', 'get');


//override authentication profile


//authentication type ='basic'/ 'oauth2'


//r.setAuthentication(authentication type, profile name);



var response = r.execute();


  gs.log("first response" + response);


var responseBody = response.getBody();


  var parser = new JSONParser();  


            var parsed = parser.parse(responseBody);  


  gs.log("responseBody" + responseBody);


    gs.log("Parse Value " + parsed.vmDiskId);


  gs.log("Test Value" + parsed.imageType);


  var httpStatus = response.getStatusCode();


  gs.log("API Response status code " + status );


       


   


  return 'ABB'; --> I have called this in client script onload(). to check my code is working .



Le me know if you face any problem



Regards,


Syed Farhan