Warning! This documentation is a work in progress. Expect things to be out of date and not actually work according to instructions.

Fetching External Web Pages and Making HTTP Calls

Stallion bundles in the third party library Unirest in order to make HTTP calls.

var Unirest = Java.type('com.mashape.unirest.http.Unirest'); // Get an HTML web page var response = Unirest.get("http://httpbin.org/html").asString(); var html = response.getBody(); assert 200 === response.getStatus(); assert html.indexOf("Herman Melville" > -1); // Get a JSON web page var response = Unirest.get("http://httpbin.org/get").asJson(); var obj = response.getBody(); // Get a JSON web page as a javascript dictionary var response = Unirest.get("http://httpbin.org/get").asString(); var data = stallion.JSON.parseMap(response.getBody()); assert "http://httpbin.org/get" === data.url // POST JSON var result = Unirest.post("http://httpbin.org/post") .queryString("name", "Mark") .field("last", "Polo") .asJson() var jsonNode = result.getBody(); assert "Polo".equals(jsonNode.getObject().get("last")); import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.HttpResponse; HttpResponse<String> httpResponse = Unirest.get("http://httpbin.org/html") .asString(); assert httpResponse.getStatus() == 200; assert httpResponse.getBody().contains("Moby Dick"); // To JSON Object HttpResponse<String> bookResponse = Unirest.get("http://httpbin.org/books/1").asString(); Book book = JSON.parse(bookResponse.getBody(), Book.class); // POST JSON HttpResponse<JsonNode> jsonResponse = Unirest.post("http://httpbin.org/post") .queryString("name", "Mark") .field("last", "Polo") .asJson(); assert "Polo".equals(jsonResponse.getBody().getObject().get("last"));

See the Unirest documentation for full use cases.

© 2024 Stallion Software LLC