Convert Figma logo to code with AI

Kong logounirest-java

Unirest in Java: Simplified, lightweight HTTP client library.

2,594
592
2,594
6

Top Related Projects

45,699

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

42,958

A type-safe HTTP client for Android and the JVM

Asynchronous Http and WebSocket Client library for Java

14,240

Vert.x is a tool-kit for building reactive applications on the JVM

Spring Framework

Mirror of Apache HttpClient

Quick Overview

The Kong/unirest-java repository is a Java-based HTTP client library that provides a simple, elegant, and lightweight interface for making HTTP requests. It is designed to be easy to use and highly customizable, making it a popular choice for developers who need to interact with web services and APIs.

Pros

  • Simplicity: Unirest-java offers a straightforward and intuitive API, making it easy for developers to quickly integrate it into their projects.
  • Flexibility: The library supports a wide range of HTTP methods, headers, and parameters, allowing developers to handle complex API interactions with ease.
  • Cross-platform compatibility: Unirest-java is a cross-platform library, making it suitable for use in a variety of development environments.
  • Asynchronous support: The library provides support for asynchronous HTTP requests, allowing developers to improve the performance and responsiveness of their applications.

Cons

  • Limited documentation: The project's documentation could be more comprehensive, which may make it challenging for new users to get started.
  • Dependency on external libraries: Unirest-java relies on several external libraries, which could increase the overall complexity and size of a project.
  • Potential performance issues: Depending on the use case, the library's performance may not be as optimal as more low-level HTTP client libraries.
  • Lack of active maintenance: The project appears to have a relatively low level of active maintenance, which could be a concern for long-term use.

Code Examples

Here are a few examples of how to use the Unirest-java library:

Making a GET request:

HttpResponse<JsonNode> response = Unirest.get("https://api.example.com/data")
    .header("accept", "application/json")
    .queryString("param1", "value1")
    .queryString("param2", "value2")
    .asJson();

Making a POST request with a JSON body:

JSONObject body = new JSONObject()
    .put("key1", "value1")
    .put("key2", "value2");

HttpResponse<JsonNode> response = Unirest.post("https://api.example.com/data")
    .header("Content-Type", "application/json")
    .body(body)
    .asJson();

Handling asynchronous requests:

Future<HttpResponse<JsonNode>> future = Unirest.get("https://api.example.com/data")
    .asJsonAsync();

// Do other work while the request is in progress
// ...

HttpResponse<JsonNode> response = future.get();

Configuring the HTTP client:

Unirest.config()
    .connectTimeout(10000)
    .socketTimeout(10000)
    .proxy("proxy.example.com", 8080)
    .setDefaultHeader("User-Agent", "My Custom User Agent");

Getting Started

To get started with Unirest-java, follow these steps:

  1. Add the Unirest-java dependency to your project's build file. For example, in a Maven project, add the following to your pom.xml:
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.13.6</version>
</dependency>
  1. Import the necessary classes in your Java code:
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
  1. Start making HTTP requests using the Unirest-java API:
HttpResponse<JsonNode> response = Unirest.get("https://api.example.com/data")
    .header("accept", "application/json")
    .asJson();

int statusCode = response.getStatus();
JsonNode body = response.getBody();
  1. Customize the HTTP client configuration as needed, such as setting timeouts, proxies, and default headers.

Competitor Comparisons

45,699

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

Pros of OkHttp

  • More actively maintained with frequent updates and improvements
  • Extensive features including connection pooling, HTTP/2 support, and WebSocket capabilities
  • Better performance and lower memory footprint

Cons of OkHttp

  • Steeper learning curve for beginners
  • Requires more boilerplate code for simple requests

Code Comparison

OkHttp:

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
    .url("https://api.example.com/data")
    .build();
Response response = client.newCall(request).execute();

Unirest:

HttpResponse<String> response = Unirest.get("https://api.example.com/data")
    .asString();

Summary

OkHttp is a more powerful and feature-rich HTTP client, offering better performance and modern protocol support. It's ideal for complex applications and those requiring fine-grained control over network operations. Unirest, on the other hand, provides a simpler API that's easier to use for basic HTTP requests, making it more suitable for smaller projects or developers who prefer a more straightforward approach. The choice between the two depends on the specific needs of your project and your familiarity with HTTP client libraries.

42,958

A type-safe HTTP client for Android and the JVM

Pros of Retrofit

  • More robust and feature-rich, with built-in support for various HTTP methods and request/response types
  • Better integration with modern Android development practices and libraries
  • Extensive documentation and large community support

Cons of Retrofit

  • Steeper learning curve, especially for beginners
  • Requires more setup and configuration compared to Unirest-Java
  • May be overkill for simple API requests

Code Comparison

Retrofit:

public interface GitHubService {
  @GET("users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);
}

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

GitHubService service = retrofit.create(GitHubService.class);

Unirest-Java:

HttpResponse<JsonNode> response = Unirest.get("https://api.github.com/users/{user}/repos")
    .routeParam("user", "octocat")
    .asJson();

Retrofit offers a more declarative approach with interface-based API definitions, while Unirest-Java provides a simpler, more straightforward method for making HTTP requests. Retrofit's approach is generally more scalable and maintainable for larger projects, but Unirest-Java may be quicker to implement for smaller applications or one-off requests.

Asynchronous Http and WebSocket Client library for Java

Pros of async-http-client

  • Highly customizable and feature-rich for advanced use cases
  • Better performance for high-concurrency scenarios
  • Supports WebSocket connections natively

Cons of async-http-client

  • Steeper learning curve due to more complex API
  • Less intuitive for simple HTTP requests
  • Requires more boilerplate code for basic operations

Code Comparison

async-http-client:

AsyncHttpClient client = Dsl.asyncHttpClient();
Future<Response> f = client.prepareGet("http://www.example.com/").execute();
Response r = f.get();

unirest-java:

HttpResponse<String> response = Unirest.get("http://www.example.com/")
  .asString();

Summary

async-http-client is a powerful library for handling complex HTTP scenarios and high-concurrency applications. It offers more advanced features and better performance in certain situations. However, it comes with a steeper learning curve and requires more code for basic operations.

unirest-java, on the other hand, provides a simpler and more intuitive API for making HTTP requests. It's easier to use for basic operations and requires less boilerplate code. However, it may not be as suitable for highly complex or high-performance scenarios.

The choice between these libraries depends on the specific requirements of your project, such as the complexity of HTTP operations, performance needs, and developer familiarity with asynchronous programming concepts.

14,240

Vert.x is a tool-kit for building reactive applications on the JVM

Pros of Vert.x

  • Highly scalable and event-driven architecture
  • Supports multiple programming languages
  • Comprehensive ecosystem with various modules

Cons of Vert.x

  • Steeper learning curve for beginners
  • More complex setup compared to Unirest
  • Requires understanding of reactive programming concepts

Code Comparison

Vert.x HTTP client example:

vertx.createHttpClient().request(HttpMethod.GET, 8080, "localhost", "/")
  .compose(request -> request.send()
    .compose(response -> response.body()))
  .onSuccess(body -> System.out.println("Response: " + body.toString()))
  .onFailure(Throwable::printStackTrace);

Unirest HTTP client example:

HttpResponse<String> response = Unirest.get("http://localhost:8080/")
  .asString();
System.out.println("Response: " + response.getBody());

Vert.x offers a more reactive and non-blocking approach, while Unirest provides a simpler, synchronous API. Vert.x is better suited for building large-scale, high-performance applications, whereas Unirest is ideal for quick and straightforward HTTP requests in smaller projects or when simplicity is preferred over advanced features.

Spring Framework

Pros of Spring Framework

  • Comprehensive ecosystem with extensive features for building enterprise applications
  • Strong community support and regular updates
  • Modular architecture allowing developers to use only needed components

Cons of Spring Framework

  • Steeper learning curve due to its vast feature set
  • Can be overkill for simple projects or microservices
  • Potential for configuration complexity in large applications

Code Comparison

Spring Framework:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

Unirest-Java:

HttpResponse<String> response = Unirest.get("http://example.com/hello")
  .header("accept", "application/json")
  .asString();
String body = response.getBody();

Spring Framework offers a more declarative approach with annotations, while Unirest-Java provides a fluent API for making HTTP requests. Spring is better suited for building full-fledged applications, whereas Unirest-Java excels in simplifying HTTP client operations.

Spring Framework is a comprehensive framework for Java application development, offering a wide range of features and modules. Unirest-Java, on the other hand, is a lightweight HTTP client library focused on simplifying RESTful communication.

Choose Spring Framework for complex, enterprise-grade applications that require a robust ecosystem. Opt for Unirest-Java when you need a simple, easy-to-use HTTP client for making API requests in your Java projects.

Mirror of Apache HttpClient

Pros of HttpComponents Client

  • More comprehensive and feature-rich, offering advanced HTTP functionality
  • Better suited for complex HTTP operations and low-level control
  • Widely adopted and battle-tested in enterprise environments

Cons of HttpComponents Client

  • Steeper learning curve due to its complexity
  • More verbose code required for simple HTTP requests
  • Heavier dependency compared to Unirest

Code Comparison

HttpComponents Client:

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet request = new HttpGet("https://api.example.com/data");
CloseableHttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);

Unirest:

HttpResponse<String> response = Unirest.get("https://api.example.com/data")
    .asString();
String result = response.getBody();

Summary

HttpComponents Client is a more powerful and flexible HTTP client library, offering extensive features and low-level control. It's ideal for complex HTTP operations and enterprise-level applications. However, it comes with a steeper learning curve and requires more verbose code for simple tasks.

Unirest, on the other hand, provides a simpler and more intuitive API, making it easier to use for basic HTTP requests. It's lightweight and requires less boilerplate code, but may lack some advanced features found in HttpComponents Client.

Choose HttpComponents Client for complex HTTP operations and fine-grained control, or Unirest for quick and simple HTTP requests in smaller projects or when ease of use is a priority.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Unirest for Java

Actions Status Maven Central Javadocs

Unirest 4

Unirest 4 is build on modern Java standards, and as such requires at least Java 11.

Unirest 4's dependencies are fully modular, and have been moved to new Maven coordinates to avoid conflicts with the previous versions. You can use a maven bom to manage the modules:

Install With Maven

<dependencyManagement>
  <dependencies>
      <!-- https://mvnrepository.com/artifact/com.konghq/unirest-java-bom --> 
      <dependency>
          <groupId>com.konghq</groupId>
          <artifactId>unirest-java-bom</artifactId>
          <version>4.4.0</version>
          <type>pom</type>
          <scope>import</scope>
      </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.konghq/unirest-java-core -->
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-java-core</artifactId>
    </dependency>
    
    <!-- pick a JSON module if you want to parse JSON include one of these: -->
    <!-- Google GSON -->
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-modules-gson</artifactId>
    </dependency>

    <!-- OR maybe you like Jackson better? -->
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-modules-jackson</artifactId>
    </dependency>
</dependencies>

🚨 Attention JSON users 🚨

Under Unirest 4, core no longer comes with ANY transient dependencies, and because Java itself lacks a JSON parser you MUST declare a JSON implementation if you wish to do object mappings or use Json objects.

Upgrading from Previous Versions

See the Upgrade Guide

ChangeLog

See the Change Log for recent changes.

Documentation

Our Documentation

Unirest 3

Maven

<!-- Pull in as a traditional dependency -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.14.1</version>
</dependency>