Sobes.tech
Junior — Middle

Have you worked with RESTful API and made HTTP requests to web servers?

sobes.tech AI

Answer from AI

Yes, experience with RESTful APIs and making HTTP requests in Java is quite common. Typically, libraries such as HttpURLConnection (standard), Apache HttpClient, or more modern ones like OkHttp are used.

Here's an example of a simple GET request using HttpURLConnection:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class RestClient {
    public static void main(String[] args) throws Exception {
        URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");

        int status = con.getResponseCode();
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder content = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        con.disconnect();

        System.out.println("Response code: " + status);
        System.out.println("Response body: " + content.toString());
    }
}

This approach allows sending requests to RESTful services, receiving responses, and processing them.

Have you worked with RESTful API and made HTTP… - sobes.tech