Created
October 25, 2018 13:28
-
-
Save vainolo/7ce77ff5925bd4bc445f0cbb92fe2721 to your computer and use it in GitHub Desktop.
Calling a Web API from Java using Unirest - Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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