0

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?

Cody Pritchard
  • 703
  • 2
  • 15
  • 31

2 Answers2

0

According to this QA: HttpClientBuilder - java.lang.NoSuchFieldError: INSTANCE you have got a mix of incompartible versions for httpcore and httpclient dependencies of Apache HTTP Client.

Advice: investigate the transient dependencies and try to resolve the conflict by removing the libraries that from your dependency list that cause that problem, or explicitly define both libraries in you project pom.xml, e.g.:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.10</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.6</version>
</dependency>

Note: I picked the latest compatible versions for httpclient library. This can change in the future or may not match your currently used httpclient version.

Vladimir L.
  • 1,116
  • 14
  • 23
0
        CloseableHttpClient httpclient = HttpClients.createDefault();

This worked for me and you dont have to open and close connection using connection manager