UniRest
Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by the Mashape team.
Unirest.post("http://httpbin.org/post") .queryString("name", "Mark") .field("last", "Polo") .asJson()
Features
- Make
GET
,POST
,PUT
,PATCH
,DELETE
,HEAD
,OPTIONS
requests - Both syncronous and asynchronous (non-blocking) requests
- It supports form parameters, file uploads and custom body entities
- Easily add route parameters without ugly string concatenations
- Supports gzip
- Supports Basic Authentication natively
- Customizable timeout, concurrency levels and proxy settings
- Customizable default headers for every request (DRY)
- Customizable
HttpClient
andHttpAsyncClient
implementation - Automatic JSON parsing into a native object for JSON responses
- Customizable binding, with mapping from response body to java Object
Installing
Is easy as pie. Kidding. It’s about as easy as doing these little steps:
With Maven
You can use Maven by including the library:
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.7</version>
</dependency>
There are dependencies for Unirest-Java, these should be already installed, and they are as follows:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
If you would like to run tests, also add the following dependency along with the others:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
Without Maven
Alternatively if you don’t use Maven, you can directly include the JAR file in the classpath:http://oss.sonatype.org/content/repositories/releases/com/mashape/unirest/unirest-java/1.4.7/unirest-java-1.4.7.jar
Don’t forget to also install the dependencies (org.json
, httpclient 4.3.6
, httpmime 4.3.6
,httpasyncclient 4.0.2
) in the classpath too.
There is also a way to generate a Unirest-Java JAR file that already includes the required dependencies, but you will need Maven to generate it. Follow the instructions at http://blog.mashape.com/post/69117323931/installing-unirest-java-with-the-maven-assembly-plugin
Creating Request
So you’re probably wondering how using Unirest makes creating requests in Java easier, here is a basic POST request that will explain everything:
HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post")
.header("accept", "application/json")
.queryString("apiKey", "123")
.field("parameter", "value")
.field("foo", "bar")
.asJson();
Requests are made when as[Type]()
is invoked, possible types include Json
, Binary
, String
, Object
.
If the request supports and it is of type HttpRequestWithBody
, a body it can be passed along with.body(String|JsonNode|Object)
. For using .body(Object)
some pre-configuration is needed (see below).
If you already have a map of parameters or do not wish to use seperate field methods for each one there is a.fields(Map<String, Object> fields)
method that will serialize each key – value to form parameters on your request.
.headers(Map<String, String> headers)
is also supported in replacement of multiple header methods.
Full Documentation @ unirest.io