How to call /bin/querybuilder.json in java CQ5

How to call querybuilder API in Java. I have tried this but i am getting no result. But when i am trying to hit in browser with this URL localhost path then the response is coming properly.

URL url = new URL("http://localhost:4502/bin/querybuilder.json?path=/content");
HttpURLConnection servletConnection = (HttpURLConnection) url.openConnection();
servletConnection.setRequestMethod("GET");
servletConnection.setDoOutput(true);
InputStream response = servletConnection.getInputStream();

Answer

Based on the code shown, most likely you don’t have a session established with your localhost. When you hit that URL in a new browser window you will get redirected to a login page where you must first login with appropriate credentials before you can see the page. But when you make the same request via Java code, you haven’t provided any credentials. So you won’t be able to get to the content.

Check our Preemptive Basic Auth with HttpUrlConnection? for info on how to provide credentials.

Leave a Reply

Your email address will not be published. Required fields are marked *