REST API GET request

now_dev
Mega Expert

Hi All,

I am trying to make a REST call using GET method and I am getting an error saying "Response: Error constructing REST Message/Method: https://041817164.dev.lab.venafi.com/vedsdk//GET".

I am trying to create one generic function REST parameters

makeRequest: function(url, method, header, body){

  var apiKey;

  var midServerName;

  var sm;

  var response;

  var gr = new GlideRecord('x_14777_venafi_tru_configurations');

  gr.query();

  while(gr.next())

  {

  apiKey = gr.getValue('api_key');

  midServerName = gr.getValue('mid_server');

  }

  sm = new sn_ws.RESTMessageV2(url, method, header, body);

  sm.setEndpoint(url);

  sm.setHttpMethod(method);

  sm.setRequestHeader(header);

  sm.setRequestBody(body);

  if (midServerName != '')

  {

  sm.setMIDServer(midServerName);

  }

  response = sm.execute();

  return response;

  },

  checkvalid: function(){

  var requestBody1;

  var response1;

  var responseBody1;

  var status1;

  var key;

  var sm;

  try{

  var gr = new GlideRecord('x_14777_venafi_tru_configurations');

  gr.query();

  while(gr.next())

  {

  key = gr.getValue('api_key');

  }

  var url = 'https://041817164.dev.lab.venafi.com/vedsdk/';

  var method = "GET";

var header = "('X-Venafi-Api-Key', key)"; // I feel the error is because of the way header is being declared but I am not sure how to fix it.

  response1 = this.makeRequest(url, method, header, '');

  responseBody1 = response1.haveError() ? response1.getErrorMessage() : response1.getBody();

  status = response1.getStatusCode();

  } catch(ex) {

  gs.addErrorMessage("Error in Key");

  responseBody1 = ex.getMessage();

  status1 = '500';

  } finally {

  gs.addInfoMessage("Response: " + responseBody1);

  requestBody1 = sm ? sm.getRequestBody1():null;

  }

  return responseBody1;

  },

Any help would be much appreciated.

1 ACCEPTED SOLUTION

makeValidRequest looks good to me. One improvement could be to to send the header itself to the needAPI.


var myheader = "'X-Venafi-Api-Key', apiKey";



So that it becomes more generic and you dont have to keep changing the function, if there are new methods in future.



You can also combine authorize() and checkvalid(), which can reduce redundancy in code. Just pass the method to one function and write the conditions accordingly.





Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

7 REPLIES 7

SanjivMeher
Kilo Patron
Kilo Patron

Does it work when you trying doing it using SOAP UI or Google Rest API?



Please mark this response as correct or helpful if it assisted you with your question.

Yes and it also works when I directly right below code



checkvalid: function(){


  var requestBody;


  var response;


  var responseBody1;


  var status;


  var sm;


  try{


 


  sm = new sn_ws.RESTMessageV2();


  sm.setEndpoint('https://041817164.dev.lab.venafi.com/vedsdk/authorize/checkvalid');


  sm.setHttpMethod('GET');


  sm.setRequestHeader('Content-Type','application/json');


  sm.setRequestHeader('X-Venafi-Api-Key',key);


  response = sm.execute();


  responseBody1 = response.haveError() ? response.getErrorMessage() : response.getBody();


  status = response.getStatusCode();


  } catch(ex) {


  gs.addErrorMessage("Error in Key");


  responseBody = ex.getMessage();


  status = '500';


  } finally {


  gs.addInfoMessage("Response: " + responseBody1);


  requestBody = sm ? sm.getRequestBody():null;


  }



  return responseBody;


  },



All I am trying to create an generic function for REST parameters and wanted to call that function in above code.


Can you try below code and check the system log for logs.



makeRequest: function(url, method, header, body){


  var apiKey;


  var midServerName;


  var sm;


  var response;


  var gr = new GlideRecord('x_14777_venafi_tru_configurations');


  gr.query();


  while(gr.next())


  {


  apiKey = gr.getValue('api_key');


  midServerName = gr.getValue('mid_server');


  }


  sm = new sn_ws.RESTMessageV2(url, method, header, body);


  sm.setEndpoint(url);


  sm.setHttpMethod(method);


  sm.setRequestHeader('Content-Type','application/json');


  sm.setRequestHeader('X-Venafi-Api-Key',header); // Pass just the key in the header parameter


  sm.setRequestBody(body); //Remove this


gs.log(' url is '+url);


gs.log('method is '+method);


gs.log('header is '+header);


  if (midServerName != '')


  {


  sm.setMIDServer(midServerName);


  }


  response = sm.execute();


  return response;


  },



Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,



I just realized that makeRequest function is not getting called. Am I calling the function in the correct way?