I am trying to send an HTTP request through Java to a ServiceNow REST API and create an event.
I have this working in Postman with:
URL: POST https://<domain>.service-now.com/api/now/table/em_event
Body:
{
"source":"Postman",
"node":"Codys Mac",
"type":"Test",
"resource":"Test",
"metric_name":"Testing",
"event_class":"Test",
"severity":"4",
"description":"This is a test event, created from REST API.",
}
and it works, no issues.
But in Java, im having nothing but problems.
The simplest example I have found that im trying to follow is here: https://kodejava.org/how-do-i-send-an-http-post-request/
And my code for this:
public void post(){
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(serviceNowURL);
List<NameValuePair> arguments = new ArrayList<>();
arguments.add(new BasicNameValuePair("source", "Sample Producer"));
arguments.add(new BasicNameValuePair("node", "Codys Mac"));
arguments.add(new BasicNameValuePair("type", "Test Event Type"));
arguments.add(new BasicNameValuePair("resource", "Test Event Resource"));
arguments.add(new BasicNameValuePair("metric_name", "Test Event Metric Name"));
arguments.add(new BasicNameValuePair("event_class", "Test Event Class"));
arguments.add(new BasicNameValuePair("sevrity", "5"));
arguments.add(new BasicNameValuePair("description", "This is a test event, created by SampleProducer App usign REST API."));
try{
String encoding = Base64.getEncoder().encodeToString((username.concat(":").concat(password)).getBytes("UTF-8"));
post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
post.setHeader(HttpHeaders.ACCEPT, "application/json");
post.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encoding);
post.setEntity(new UrlEncodedFormEntity(arguments));
HttpResponse response = client.execute(post);
}
catch(Exception e){
System.out.println(ExceptionUtils.getRootCauseMessage(e));
}
}
But whenever I run this, i encounter an exception on the first line of HttpClientBuilder.create().build()
of java.lang.NoSuchFieldError: INSTANCE
and i have no idea why.
Does anyone know what im doing wrong, or another way I can approach this?