javaspringrest-clientunirestunirest-java

Unirest Java Client : kong.unirest.UnirestException: org.apache.http.client.ClientProtocolException


I am using Unirest Java client which I am using like below to connect to external API:

 public static String loginAsAdmin(String authenticationURL , String userName , String password){
        Map<String,String> creds =  new HashMap<>();
        creds.put("username", userName);
        creds.put("password",password);
        HttpResponse<JsonNode> jsonResponse
                = Unirest.post(authenticationURL).header("Content-Type","application/json")
                .body(creds)
                .asJson();

        return jsonResponse.getBody().toString();
    }

but while I am running code I am getting following error:

kong.unirest.UnirestException: org.apache.http.client.ClientProtocolException

Stacktrace:

kong.unirest.UnirestException: org.apache.http.client.ClientProtocolException

    at kong.unirest.DefaultInterceptor.onFail(DefaultInterceptor.java:43)
    at kong.unirest.apache.ApacheClient.request(ApacheClient.java:133)
    at kong.unirest.BaseRequest.asJson(BaseRequest.java:232)

For information ( I am using following Unirest dependency ):

 <dependency>
            <groupId>com.konghq</groupId>
            <artifactId>unirest-java</artifactId>
            <version>3.6.00</version>
        </dependency>

Solution

  • Strange this error message was actually misleading. Cause of this issue was wrong endpoing URL, which in turn was giving 404. I replace with proper URL and its working fine. Perhaps unirest error messaging needs to treat this properly to denote proper mistake.