Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Access External API from Business Rule

thisisauniqueus
Giga Expert

Hi,

Can someone please advise me on how can i access a REST full API's endpoint? I have to access an endpoint say for example

http://someotherdomain/api/doSomething/1

is there a way that i can do it in a business rule using the REST Client?

Regards

1 ACCEPTED SOLUTION

Hi John,



You have everything setup properly except for two notes.


1) I had a typo in mine: setHTTPMethod should be setHttpMethod


2) if you are going to use setQueryParameter(), you don't need to set the parameters on the endpoint url. (Anything after a "?" is a parameter)


      rm.setEndpoint('http://api.tvmaze.com/search/shows');


      rm.setQueryParameter("q", "girls");


     


Otherwise if you do both the endpoint would end up looking like this http://api.tvmaze.com/search/shows?q=girls?q=girls


View solution in original post

11 REPLIES 11

ChrisBurks
Giga Sage

It would be more versatile if you use the outbound Rest Message Application: Outbound Rest Web Service or place the below in a script include


But if it's just a one off then:


https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=c_RESTMessageV2API



var rm = new sn_ws.RESTMessageV2();


rm.setHTTPMethod('post');


rm.setEndpoint('http://.....');


rm.setRequestHeader("headerName", "headerValue"); // ie. ("Accept", "Application/json")


rm.setBasicAuth("username", "password");


rm.setRequestBody('{stringified json stuff here}');


var response = rm.execute();




Getting the response


https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=c_RESTResponseV2API



var responseBody = response.getBody();



That's a basic setup



Hope that helps


Thanks Chirs for your time. I'm trying the code below without any success. Can you please have a look



the endpoint im trying to access is : http://api.tvmaze.com/search/shows?q=girls



(function executeRule(current, previous /*null when async*/) {


  gs.log("[REST API CALL] init");


  try{


  var rm = new sn_ws.RESTMessageV2();


  rm.setHTTPMethod('get');


  rm.setEndpoint('http://api.tvmaze.com/search/shows?q=girls ');


  rm.setQueryParameter("q", "girls");


  //rm.setRequestHeader("Accept", "Application/json"); // ie. ("Accept", "Application/json")


  //rm.setBasicAuth("username", "password");


  //rm.setRequestBody('{q:"girls"}');


  var response = rm.execute();


  var responseBody = response.getBody();


  gs.log("[REST API CALL] status code: "+response.getStatusCode());


  }catch(ex){


  gs.log("[REST API CALL] exception"+ex);


  }



})(current, previous);



im getting status code 0



Regards


Hi John,



You have everything setup properly except for two notes.


1) I had a typo in mine: setHTTPMethod should be setHttpMethod


2) if you are going to use setQueryParameter(), you don't need to set the parameters on the endpoint url. (Anything after a "?" is a parameter)


      rm.setEndpoint('http://api.tvmaze.com/search/shows');


      rm.setQueryParameter("q", "girls");


     


Otherwise if you do both the endpoint would end up looking like this http://api.tvmaze.com/search/shows?q=girls?q=girls


that did the trick. Can't thank you enough for your support.



Best Regards