- 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
‎10-06-2017 12:40 PM
Hi ChrisB,
I am trying to do the same thing that John was doing but instead, make an API call to GitLab to pull information on repositories and then store that information back into tables in ServiceNow. I tried using his code and am able to make a successful call to his API and retrieve that information, however, when I try to make a call to GitLab I receive a Status Code of 0. Here is my code with everything except I took out my GitLab private token for obvious reasons.
(function executeRule(current, previous /*null when async*/) {
try {
var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod("get");
rm.setEndpoint('http://gitlab.rhino.root/api/v3/projects?private_token=PRIVATE_TOKEN');
var response = rm.execute();
var responseBody = response.getBody();
gs.addErrorMessage(gs.getMessage("Status Code: " + response.getStatusCode();));
gs.addErrorMessage(gs.getMessage("Response: " + responseBody));
} catch(ex) {
gs.log("[REST API CALL] exception"+ex);
}
}) (current, previous);
I believe I have everything correct and that this should be able to make a successful API call. If I search the GitLab link with the private token as a normal web search it returns the JSON response, so I'm not sure why it isn't working here. Any tips / advice as to what I can do to solve this would be much appreciated.
I am currently just printing the response to an error message after the user updates or inserts into a table.
Thanks!
Doug
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2017 01:37 PM
Hi Doug,
This may not be the resolution but the first thing that I noticed is that you're using "http" instead of "https". This would be fine if that is how the api is set up but if it isn't then you'll have to use "https".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2017 02:05 PM
Hi ChrisB,
I actually realized my problem. I am trying to access a private GitLab account that is behind a firewall. I tried making an API call to a normal GitLab account that isn't behind a firewall and was able to get the information just fine. I was reading up on using a mid-server, is this the only way to work around this obstacle?
Thanks!
Doug
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2017 02:16 PM
I don't know if that is the only way but it is a good way if the administrator of the firewall won't open up or adjust the firewall to allow access. The mid-server needs to be on the same side of the firewall as the gitlab account. Also realize that "mid-server" doesn't mean a full-blown server. The "mid-server" is software that will sit on some computer within the same network.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2017 10:16 PM
1. Please create the end point (ie) go to system navigator-->Outbound-->Soap Message or Rest Message
2. create the end point. Add the parameters what ever needed.
3. Substitute where ever you are calling the end point.
below is the sample script..
var object_data = "";
var sValues = "";
var sJoin = "";
var fields = current.getFields();
for (var i = 0; i < field.size(); i++){
var field = fields.get(i);
var name = field.getName(i);
var value = field.getDisplayValue();
sValues = sValues + sJoin + "'" + name + "':'" + GlideString.escapeHTML(valye) + "'";
sJoin = ",";
}
object_data = "{" + sValues + "}";
gs.log("object data --->" +object_data);
var obj = new JSON().encode(object_data);
var r = new sn_ws.RESTMessageV2('End Point Name', 'post");
r.setStringParameter("InstanceName",gs.getProperty('instance_name'));
r.setStringParameter("direction","Outbound");
r.setStringParameter("customer_name",current.company.getDisplayValue());
r.setStringParameter("customer_id",current.company.sys_id);
r.setStringParameter("Object_id",current.sys_id);
r.setStringParameter("Object_name",current.number);
r.setStringParameter("Object_type",current.sys_class_name);
r.setStringParameter("operation_name",operation_name);
r.setXMLParameterNoEscape("object_data",obj);
r.setHttpTimeout(10000);
var response=r.execute();
var responseBody= response.getBody();
status = response.getStatusCode();
}
catch(e)
{
gs.log("exception" + e)
}