Search code examples
javamongodbrestheart

Using Java (HttpURLConnection) to authenticate to Restheart (for Mongodb)


I am using restheart to provide a restful interface to mongodb. The interface is set up and running and provides the correct answer if a GET request is sent through Chrome. However if I use the following java code using a HttpURLConnection I get a 201 response with no content.

try {
    videos = new URL("http://www.example.com:8080/myflix/videos");
    } catch (Exception et) {
        System.out.println("Videos URL is broken");
        return null;
    }
    HttpURLConnection hc = null;
    try {
        hc = (HttpURLConnection) videos.openConnection();
        String login="admin:admin"; 
        final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
        final String encoded = Base64.getEncoder().encodeToString(authBytes);
        hc.addRequestProperty("Authorization", "Basic "+encoded);
        hc.setDoInput(true);
        hc.setDoOutput(true);
        hc.setUseCaches(false);
        hc.setRequestMethod("GET");
        hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
        hc.setRequestProperty("Content-Type", "application/json");
        hc.setRequestProperty("Accept", "application/json,text/html,application/hal+json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*");
    } catch (Exception et) {
        System.out.println("Can't prepare http URL con");
        return (null);
    }
    BufferedReader br = null;
    try {
        OutputStreamWriter writer = new OutputStreamWriter(
                hc.getOutputStream());
    } catch (Exception et) {
        System.out.println("Can't get reader to videos stream");
    }
    String inputLine;
    String sJSON = null;

    try {
        int rc = hc.getResponseCode();

What is the correct way to authenticate using Java to the resthert interface? (Details on the restheart authentication is here Restheart authentication


Solution

  • I made few changes (look for inline comments starting with <==) and it works:

    The way you generate the authentication request header is correct. When I run your code I actually got 415 Unsupported Media Type, that went away commenting out hc.setDoOutput(true). A GET is a input operation, in fact you were also trying to get an OutStream from the connection: you need to get an InputStream actually.

        URL url;
    
        try {
            url = new URL("http://127.0.0.1:8080/test/huge");
        } catch (Exception et) {
            System.out.println("Videos URL is broken");
            Assert.fail(et.getMessage());
            return;
        }
    
        HttpURLConnection hc = null;
    
        try {
            hc = (HttpURLConnection) url.openConnection();
    
            String login = "admin:admin";
            final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
    
            final String encoded = Base64.getEncoder().encodeToString(authBytes);
            hc.addRequestProperty("Authorization", "Basic " + encoded);
    
            System.out.println("Authorization: " + hc.getRequestProperty("Authorization"));
    
            hc.setDoInput(true);
            //hc.setDoOutput(true); <== removed, otherwise 415 unsupported media type
            hc.setUseCaches(false);
    
            hc.setRequestMethod("GET");
            hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
            hc.setRequestProperty("Accept", "application/json,text/html,application/hal+json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*");
        } catch (Exception et) {
            System.out.println("Can't prepare http URL con");
        }
    
        System.out.println(hc.toString());
    
        BufferedReader br = null;
    
        try {
            InputStreamReader reader = new InputStreamReader(hc.getInputStream()); // <== the request is a GET, data is in input
        } catch (Exception et) {
            System.out.println("Can't get reader to videos stream");
        }
    
        int rc = hc.getResponseCode();
    
        System.out.println("response code: " + rc);
        System.out.println("response message: " + hc.getResponseMessage());
    
        Assert.assertEquals(200, rc);