Home / CodeScripts / Scripts / Making an HTTP GET Request and Parsing Response in Java

Making an HTTP GET Request and Parsing Response in Java

Making an HTTP GET Request and Parsing Response in Java
  • Category Scripts
  • Type Script
  • Platform Cross-platform
  • Language Java
  • Price Free
  • Copy 4 759
  • Comments 0
Go to Code
Making an HTTP GET Request and Parsing Response in Java

Making an HTTP GET Request and Parsing Response in Java

Copy this Java snippet on Clayi Code — one click, no account required. Free code examples with copy-ready formatting — built for developers who want speed without hunting forums.

What it does and when to use

A working code example you can drop into your project — automation, algorithms, file handling, or application logic. Use it as a starting point when you want boilerplate instead of writing everything from zero.

How to copy from Clayi Code

Copy the snippet, save with the right file extension, install any listed dependencies, and run with your usual toolchain.

Run it safely

Match your installed language version and packages. Check import, require, or using lines — install missing libraries first. Run in a sandbox or staging folder before executing with admin or network access.

Script reference

Category: Scripts. Language: Java. Platform: Cross-platform. Format: Script.

Popularity
0%
  • Votes: 465
  • Comments: 0

Making an HTTP GET Request and Parsing Response in Java

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class ApiClient {
    public static void main(String[] args) {
        // Create a modern standard HttpClient instance
        HttpClient client = HttpClient.newHttpClient();
        
        // Build the HTTP GET request with an explicit target URL
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://github.com"))
                .header("Accept", "application/json")
                .GET()
                .build();

        try {
            // Send request and fetch response body as string
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            System.out.println("Status Code: " + response.statusCode());
            System.out.println("Response Body: " + response.body());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.

Similar Snippets

Populer Snippets

There are no comments yet :(

Making an HTTP GET Request and Parsing Response in Java
Tell us what you think about "Making an HTTP GET Request and Parsing Response in Java"
Information
Users of Guests are not allowed to comment this publication.