Intro

Given that I am a web person and that I recently learned how to build a REST API in NodeJS, I started getting curious about how sending HTTP requests would work in other languages and in particular Java. I found that the process is not nearly as awesome as it is in other languages. Fortunately, I found an awesome library called Unirest. Unirest does exactly what I wanted, but since it is fairly new there isn't much documentation on using it. After some trial and error, I found out how to do what I wanted. In particular, I found out how to make it interact with my Node REST server.

Setup

First things first, how do you install it? Well if you are using a Maven project, you must simply add it to the pom.xml as a dependency.

<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>

After adding it to your pom.xml you must then import it with Maven. In IntelliJ this can be done by right-clicking on the pom.xml file in the Project Explorer -> hovering over Maven -> and selecting reimport. Unirest is now ready to be used.

Making the requests

It's time to start interacting with the backend. but in-order to do so we first need to import it in Java.

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import org.json.JSONObject;

Now we need some data to send. For demonstration purposes, I will be creating a new device using a POST request. Let's create a JSON Object with the fields that need to be populated.

//The Data you want to send in JSON format
JSONObject deviceObj = new JSONObject()
.put("Device_Type_ID", 1)
.put("Device_Name", "name")
.put("Communication_Protocol_ID", 1)
.put("Icon_ID", 1)
.put("Location_ID", 1);

Now that the data is ready, it is time to make the request. This is super simple with UniRest. All that is needed is to create a response object where we can store the result after the HTTP request is complete. In this case, I will send a POST request to insert a device into the database. The information will be contained in the body of the HTTP request. In Unirest the JSONObject can simply be passed as an argument into the body method.

//The request you are sending to the server
HttpResponse<JsonNode> jsonResponse = Unirest.post("https://meshversion2-johnnyvf24.c9users.io/devices")
.header("Content-Type", "application/json")
.body(deviceObj)
.asJson();

After the request is made the HTTP status code can be checked to make sure everything went alright. 

//The device was inserted successfully
if(jsonResponse.getStatus() == 200) {
//Request was successful
} else if(jsonResponse.getStatus() == 400) {
//Something went wrong, tell the user there was an error
}

And in case further confirmation is required we will make a GET request to the same URL to see all the devices that are in the database.

//GET ALL the devices in the database
HttpResponse<JsonNode> response = Unirest.get("https://meshversion2-johnnyvf24.c9users.io/devices")
.header("Content-Type", "application/json")
.header("accept", "application/json")
.asJson();

//Print out all the devices returned, a big string of JSON
System.out.println(response.getBody());

//Put the devices into a JSON Object
JSONObject res = new JSONObject(response.getBody());

//Print out the response in a nicer format
System.out.println(res.toString(2));

The results are below:

[{"Communication_Protocol_ID":1,"Location_ID":2,"Device_Name":"toaster","Device_ID":2,"Device_Type_ID":1,"Icon_ID":2},{"Communication_Protocol_ID":1,"Location_ID":1,"Device_Name":"thermostat","Device_ID":3,"Device_Type_ID":1,"Icon_ID":1},{"Communication_Protocol_ID":1,"Location_ID":2,"Device_Name":"microwave","Device_ID":4,"Device_Type_ID":1,"Icon_ID":2},{"Communication_Protocol_ID":2,"Location_ID":1,"Device_Name":"Test","Device_ID":5,"Device_Type_ID":1,"Icon_ID":1},{"Communication_Protocol_ID":1,"Location_ID":1,"Device_Name":"Device12","Device_ID":6,"Device_Type_ID":1,"Icon_ID":1},{"Communication_Protocol_ID":1,"Location_ID":1,"Device_Name":"name","Device_ID":7,"Device_Type_ID":1,"Icon_ID":1},{"Communication_Protocol_ID":1,"Location_ID":1,"Device_Name":"name","Device_ID":8,"Device_Type_ID":1,"Icon_ID":1}]
{"array": [
  {
    "Communication_Protocol_ID": 1,
    "Location_ID": 2,
    "Device_Name": "toaster",
    "Device_ID": 2,
    "Device_Type_ID": 1,
    "Icon_ID": 2
  },
  {
    "Communication_Protocol_ID": 1,
    "Location_ID": 1,
    "Device_Name": "thermostat",
    "Device_ID": 3,
    "Device_Type_ID": 1,
    "Icon_ID": 1
  }