Unirest gives NoSuchMethodError when using asString method

Tanner Barrett :

I am trying to do a simple get to an endpoint and get the response body as and HttpResponse using the asString() method. The code compiles, but bugs out at runtime saying there is no such method error.

I imported Unirest into my maven build and I have been following the documentation here

HttpResponse<String> response = Unirest.get("http://mywebsite/post")
                  .basicAuth("myapiuser", "mypassword")
                  .asString();

And this is the stack trace:

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.http.client.config.RequestConfig$Builder.setNormalizeUri(Z)Lorg/apache/http/client/config/RequestConfig$Builder;
    at kong.unirest.apache.DefaultFactory.tryNormalize(DefaultFactory.java:47)
    at kong.unirest.apache.DefaultFactory.apply(DefaultFactory.java:42)
    at kong.unirest.apache.DefaultFactory.apply(DefaultFactory.java:32)
    at kong.unirest.apache.RequestPrep.getHttpRequestBase(RequestPrep.java:88)
    at kong.unirest.apache.RequestPrep.prepare(RequestPrep.java:69)
    at kong.unirest.apache.ApacheClient.request(ApacheClient.java:122)
    at kong.unirest.BaseRequest.asString(BaseRequest.java:177)
    at com.ideiio.testdataloader.TestDataLoader.testRestGet(TestDataLoader.java:62)
    at com.ideiio.testdataloader.TestDataLoader.main(TestDataLoader.java:34)

I can't determine if this is just an issue with missing dependencies on my part (I am quite new to maven) or if it is an issue with the code itself (although, the Unirest call is almost an exact copy from the docs).

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ideiio.testdataloader</groupId>
  <artifactId>TestDataLoader</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>TestDataLoader</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-csv</artifactId>
      <version>1.6</version>
    </dependency>

    <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>3.0.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>com.konghq</groupId>
      <artifactId>unirest-java</artifactId>
      <version>2.3.11</version>
    </dependency>
  </dependencies>

  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>
</project>
Ottoniel Domínguez González :

It is a dependences issue, I fixed it adding a newer version of httpclient in pom. 4.5.9 which overides the 4.5.6.

<dependency>
           <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.9</version>
        </dependency>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135055&siteId=1