Skip to content

Instantly share code, notes, and snippets.

@bethkrish
Created December 14, 2020 09:27
Show Gist options
  • Save bethkrish/21efeab79ef50813754c709509a7945f to your computer and use it in GitHub Desktop.
Save bethkrish/21efeab79ef50813754c709509a7945f to your computer and use it in GitHub Desktop.
Example API Client in JAVA - Standalone Application
package io.learn.apiclient;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class APIConsumer {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient httpClient = HttpClient.newBuilder().build();
HttpRequest httpRequest = HttpRequest.newBuilder(URI.create("https://reqres.in/api/users?page=2")).build();
HttpResponse<String> response = httpClient.send(httpRequest, BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment