- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2018 01:53 PM
Hello Developer Community.
I am looking for some help with integrating some event sources into ServiceNow via Java application which will allow me to do some filtering. I have the API from other tools and have searched/ researched different ways to post data to various tables in Service Now.
Also, to make matters worse, I am a total noob at programming. Most of my background has been around shell and some python scripting. I have my Java class setup properly AFAIK, but I don't ever see even my code create a connection or error out.
The process is that this part of the code is supposed to take in JSON message and post it to ServiceNow em_event table.
I was wondering if I have this setup correctly, I am using basic authentication and if I have the correct headers to successfully make this connection to ServiceNow.
try {
URL url = new URL(SnowEMURL);
String authStr = SnowEMUser + ":" + SnowEMPassword;
String authEncoded = Base64.encodeBytes(authStr.getBytes());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// connection.setConnectTimeout(CONNECT_TIMEOUT);
// connection.setReadTimeout(READ_TIMEOUT);
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic " + authEncoded);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
//Start Content Wrapper
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
bw.write(postData);
bw.flush();
bw.close();
logger.log(1, "Posted transaction to SNOW");
//Closing Content Wrapper and getting result
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// OK
logger.log(1, "[SnowProbe]Response OK from SNOW" + connection.getResponseCode());
// otherwise, if any other status code is returned, or no status
// code is returned, do stuff in the else block
} else {
logger.log(1, "[SnowProbe]-------------------------------");
logger.log(1, "[SnowProbe]SNOW Response" + connection.getResponseCode());
logger.log(1, "[SnowProbe]Response Body from SNOW" + connection.getResponseMessage());
}
} catch (MalformedURLException e) {
logger.log(0, "[SnowProbe]Malformed URL Exception" + e.toString());
e.printStackTrace();
}
One other thing, I am using a groovy script to post alerts from another source and a simple https://username:password@https://<snow instance url>/api/tablename works just fine. When I try that route in my java class i get an authorization fail error.
Anyways, hoping I can get some help / direction / guidance here.
thanks!
Dan
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2018 06:49 PM
Generally when I'm working with webservices I try to get them to work out of servicenow first so I can truely understand them.
That being said, the rect client i use is called "Insomnia" and has a neato feature to generate the code to make the call you're making in a bunch of languages.
So for Java OkHttp;
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"short_description\":\"test\"\n}");
Request request = new Request.Builder()
.url("https://dev00000.service-now.com/api/now/table/em_event")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Basic YOURBASE64dUSER:PASSSTRING")
.addHeader("accept", "application/json")
.build();
Response response = client.newCall(request).execute();
Or Java Unirest
HttpResponse<String> response = Unirest.post("https://dev00000.service-now.com/api/now/table/em_event")
.header("content-type", "application/json")
.header("authorization", "Basic NEVERGUNNAGETIT")
.header("accept", "application/json")
.body("{\n\t\"short_description\":\"test\"\n}")
.asString();
Do either of these methods work for you?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2018 06:49 PM
Generally when I'm working with webservices I try to get them to work out of servicenow first so I can truely understand them.
That being said, the rect client i use is called "Insomnia" and has a neato feature to generate the code to make the call you're making in a bunch of languages.
So for Java OkHttp;
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n\t\"short_description\":\"test\"\n}");
Request request = new Request.Builder()
.url("https://dev00000.service-now.com/api/now/table/em_event")
.post(body)
.addHeader("content-type", "application/json")
.addHeader("authorization", "Basic YOURBASE64dUSER:PASSSTRING")
.addHeader("accept", "application/json")
.build();
Response response = client.newCall(request).execute();
Or Java Unirest
HttpResponse<String> response = Unirest.post("https://dev00000.service-now.com/api/now/table/em_event")
.header("content-type", "application/json")
.header("authorization", "Basic NEVERGUNNAGETIT")
.header("accept", "application/json")
.body("{\n\t\"short_description\":\"test\"\n}")
.asString();
Do either of these methods work for you?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2018 07:19 AM
Hello,
thanks for this information. i have been testing with Postman and I get post successful with a response using my REST code.
I am not too familiar with Postman and certainly not all with "Insomnia". I am going to download Insomnia for testing now. I am pretty sure between the information you have provided will help me resolve the issue around posting to em_event table.
Along with this, I tried to run the jar file on the tool server and I am getting a jar is corrupted error. So my next steps are going to be, fix the REST/JSON post in the main class and then work on getting the JAR file to execute via command line on the server.
Thanks again for setting me in the right direction,
Best Regards,
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2018 02:54 PM
Just wanted to close this off, and respond with the fix I was able to implement using the tool "Insomnia" that Jace posted above.
My code was missing -
URL url = new URL(SnowEMURL);
String authStr = SnowEMUser + ":" + SnowEMPassword;
String authEncoded = Base64.encodeBytes(authStr.getBytes());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
// connection.setConnectTimeout(CONNECT_TIMEOUT);
// connection.setReadTimeout(READ_TIMEOUT);
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic " + authEncoded);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
//Start Content Wrapper
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
bw.write(SnowEMJSON);
bw.flush();
bw.close();
connection.setDoOutput(true);
Once I started testing, got the error and was able to search on StackTrace. This was the post error, then the main issue with the jar file executable was not able to run, but that was resolved by me just starting over and rebuilding all the class files step by step as the probe SDK defined.
I will go back and analyze what I did wrong in building the artifact earlier but for now all is good. Now I am off to formatting the hasmap / JSON to map to the em_event fields and some other issues with same alert being posted over and over again.
Thanks!
Dan