- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2017 06:33 AM
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.
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2017 07:32 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-12-2017 07:37 AM
Try below
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('Key is '+header);
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 key , ''); You are calling it here, instead of header, just pass the key
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;
},
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2017 01:58 AM
Hi Sanjiv,
I am able to run above code and also tried to optimize the code.
I have created on generic function makeValidRequest for RESTMessageV2 and I am calling this function in all of my other functions.
Now each 'POST' or 'GET' request has a different set of RequestHeader. Currently, I have created one parameter 'needAPI' and on the basis of true/false value I am setting request headers. Is there any better approach that you can suggest me pls? Maybe storing RequestHeader in an array or something?
At the moment there is only two function, one for 'POST' n other for 'GET' but in future, there will be more functions. In that case, I am not sure how do I set Requestheaders for each function using the generic function.
Thanks in advance
makeValidRequest: function(url, method, body, needAPI){
var apiKey;
var sm;
var response;
var responseBody;
var requestBody;
try
{
var gr = new GlideRecord('x_14777_venafi_tru_configurations');
gr.query();
while(gr.next())
{
apiKey = gr.getValue('api_key');
}
sm = new sn_ws.RESTMessageV2();
sm.setEndpoint(url);
sm.setHttpMethod(method);
if (needAPI == true)
{
sm.setRequestHeader('X-Api-Key', apiKey);
sm.setRequestHeader('Authorization', '123456');
}
else
{
sm.setRequestHeader('Content-Type','application/json');
}
if (body != '')
{
sm.setRequestBody(body);
}
response = sm.execute();
}catch(ex){
responseBody = ex.getMessage();
}finally{
requestBody = sm ? sm.getRequestBody():null;
}
return response;
},
----------------------
Entire code
ar Venafi_RESTMessageScripted = Class.create();
Venafi_RESTMessageScripted.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
authorize: function(){
var requestBody;
var response;
var responseBody;
var status;
var url;
var uname;
var pwd;
var sm;
try{
var gr = new GlideRecord('x_14777_venafi_tru_configurations');
gr.query();
while(gr.next())
{
uname = gr.getValue('username');
pwd = gr.getValue('password');
var ven_url = gr.getValue('venafi_instance_url');
url = (ven_url+ 'authorize');
}
var username = '{"Username":"'+uname.toString()+'"}';
var password = '{"Password":"'+pwd.toString()+'"}';
var body = username+password;
response = this.makeValidRequest(url, 'POST', body, false);
responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();
status = response.getStatusCode();
} catch(ex){
responseBody = ex.getMessage();
status = '500';
} finally{
//gs.addInfoMessage("Response: " + responseBody);
requestBody = sm ? sm.getRequestBody():null;
}
return responseBody;
},
checkvalid: function(){
var requestBody;
var response;
var responseBody;
var status;
var header;
var sm;
try
{
var gr = new GlideRecord('x_14777_venafi_tru_configurations');
gr.query();
while(gr.next())
{
var ven_url = gr.getValue('venafi_instance_url');
url = (ven_url+ 'authorize/checkvalid');
}
response = this.makeValidRequest(url, 'GET', '', true);
responseBody = 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: " + responseBody);
requestBody = sm ? sm.getRequestBody():null;
}
return status;
},
makeValidRequest: function(url, method, body, needAPI){
var apiKey;
var sm;
var response;
var responseBody;
var requestBody;
try
{
var gr = new GlideRecord('x_14777_venafi_tru_configurations');
gr.query();
while(gr.next())
{
apiKey = gr.getValue('api_key');
}
sm = new sn_ws.RESTMessageV2();
sm.setEndpoint(url);
sm.setHttpMethod(method);
if (needAPI == true)
{
sm.setRequestHeader('X-Venafi-Api-Key', apiKey);
}
else
{
sm.setRequestHeader('Content-Type','application/json');
}
if (body != '')
{
sm.setRequestBody(body);
}
response = sm.execute();
}catch(ex){
responseBody = ex.getMessage();
}finally{
requestBody = sm ? sm.getRequestBody():null;
}
return response;
},
type: 'Venafi_RESTMessageScripted'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2017 07:32 AM
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.