Java POST request using Unirest

In this post we will provide an example on how to make a POST request using Unirest.


You will need to add the following dependency to your maven project

POST request using Unirest:

Below is a simple example on how to send a HTTP Post request.

As you can see the Unirest api is very fluid and easy to read. We are passing the headers and payload using the headers() and body() methods respectively. The HTTP POST request is invoked when you call the asJson() method (other methods include as asBinary(), asObject() and asString()).

Once you have the response object you can access the response status code, body and headers via the getStatus(), getBody() and getHeaders() methods respectively.

Async POST request using Unirest:

Unirest allows you to make the HTTP Post request asynchronously by using the asJsonAsyn() method. This can be done in two ways. You can handle a futures object or implement the callback parameter passed into the asJsonAsync() method. Below I have provided an example for both.

Example 1:

Note the above example responseFuture.get() is a blocking call and it will wait for the HTTP call to complete before it returns. You can specify a wait period if desired.

Example 2:

The next example below, invokes the same request but we pass it a callback object to handle the HTTP response, once the request has completed.

In conclusion, I hope you found this post useful and it helps you understand how to use the Unirest library.


Similar posts:

  1. Java GET request using Unirest
  2. Calling a REST API with CURL
  3. HTTP Post request using HttpClient
  4. PUT vs POST in REST


  1. Unirest API documentation



Leave a Reply

Your email address will not be published. Required fields are marked *