Documentation
Install With Maven:
<!-- Pull in as a traditional dependency -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.11.03</version>
</dependency>
<!-- OR as a snazzy new standalone jar with shaded dependencies -->
<dependency>
<groupId>com.konghq</groupId>
<artifactId>unirest-java</artifactId>
<version>3.11.03</version>
<classifier>standalone</classifier>
</dependency>
Copy
Upgrading from Previous Versions
See the Upgrade Guide
ChangeLog
See the Change Log for recent changes.
Requests
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> response = Unirest.post("http://httpbin.org/post")
.header("accept", "application/json")
.queryString("apiKey", "123")
.field("parameter", "value")
.field("firstname", "Gary")
.asJson();
Copy
Requests are made when as[Type]()
is invoked, possible types include Json
, String
, Object
Empty
and File
.
Route Parameters
Sometimes you want to add dynamic parameters in the URL, you can easily do that by adding a placeholder in the URL, and then by setting the route parameters with the routeParam
function, like:
Unirest.get("http://httpbin.org/{fruit}")
.routeParam("fruit", "apple")
.asString();
// Results in `http://httpbin.org/apple`
Copy
The placeholder {fruit}
will be replaced with apple
.
The placeholder’s format is as easy as wrapping in curly braces: {custom_name}
All param values will be URL-Encoded for you
Default Base URLs
You can configure a default base URL to be used for all requests that do not contain a full URL.
This configuration will result in a GET to “http://homestar.com/runner”
Unirest.config().defaultBaseUrl("http://homestar.com");
Unirest.get("/runner").asString();
Copy
Query Parameters
Query-string params can be built up one by one
Unirest.get("http://httpbin.org")
.queryString("fruit", "apple")
.queryString("droid", "R2D2")
.asString();
// Results in "http://httpbin.org?fruit=apple&droid=R2D2"
Copy
Again all param values will be URL-Encoded.
You can also pass in query strings as arrays and maps:
Unirest.get("http://httpbin.org")
.queryString("fruit", Arrays.asList("apple", "orange"))
.queryString(ImmutableMap.of("droid", "R2D2", "beatle", "Ringo"))
.asString();
// Results in "http://httpbin.org?fruit=apple&fruit=orange&droid=R2D2&beatle=Ringo"
Copy
Headers
Request headers can be added with the header
method.
Unirest.get("http://httpbin.org")
.header("Accept", "application/json")
.header("x-custom-header", "hello")
.asString();
Copy
Basic Authentication
Unirest exposes a shortcut for doing basic auth when you need to. Unirest handles the Base64 encoding part. Please make sure you are always doing this over HTTPS!
Unirest.get("http://httpbin.org")
.basicAuth("user", "password1!")
.asString();
// this adds the header "Authorization: Basic dXNlcjpwYXNzd29yZDEh"
Copy
Body Data
Entity Bodies
You can post entity objects as the full body easily. This is the default behavior of most REST services.
Unless you specify otherwise the default Content-Type
is text/plain; charset=UTF-8
Unirest.post("http://httpbin.org")
.body("This is the entire body")
.asEmpty();
Copy
You can also post as a Object that is serialized using a configured ObjectMapper. (see Object Mappers for implementation details. Unirest comes with a default mapper that will serialize to json using the popular Google Gson library
Unirest.post("http://httpbin.org")
.header("Content-Type", "application/json")
.body(new SomeUserObject("Bob"))
.asEmpty();
// This will use Jackson to serialize the object into JSON.
Copy
JSON Patch Bodies
Unirest has full native support for JSON Patch requests (RFC-6902 see http://jsonpatch.com/) Per the spec, the default Content-Type
for json-patch is application/json-patch+json
Unirest.jsonPatch("http://httpbin.org")
.add("/fruits/-", "Apple")
.remove("/bugs")
.replace("/lastname", "Flintstone")
.test("/firstnam