- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2017 03:03 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2017 08:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2017 08:34 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2017 06:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2017 08:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-23-2017 10:38 AM
that did the trick. Can't thank you enough for your support.
Best Regards