Skip to content

Instantly share code, notes, and snippets.

@vainolo
Created October 25, 2018 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vainolo/7ce77ff5925bd4bc445f0cbb92fe2721 to your computer and use it in GitHub Desktop.
Save vainolo/7ce77ff5925bd4bc445f0cbb92fe2721 to your computer and use it in GitHub Desktop.
Calling a Web API from Java using Unirest - Example
package com.vainolo.examples.javahttpapiclient;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
public class JavaHTTPAPIClient {
public void getQuestionsUsingUnirest() throws Exception {
HttpResponse<JsonNode> response = Unirest.get("https://api.stackexchange.com/2.2/questions").
header("accept", "application/json").
queryString("order","desc").
queryString("sort", "creation").
queryString("filter", "default").
queryString("site", "stackoverflow").
asJson();
System.out.println(response.getBody().getObject().toString(2));
}
public static void main(String args[]) throws Exception {
JavaHTTPAPIClient client = new JavaHTTPAPIClient();
client.getQuestionsUsingUnirest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment