0

I would like to fetch the list of installed packages in my AEM server using this CURL command:

curl -u admin:admin http://localhost:4502/crx/packmgr/service.jsp?cmd=ls

I have to do this in a servlet and I have written the following code:

URL url = new URL("http://localhost:4502/crx/packmgr/service.jsp?cmd=help");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = reader.readLine();
while(line!=null) { 
    res.getWriter().println(line); 
    line=reader.readLine(); 
}

I am a newbie to the CURL commands and don't know how to put the -u flag for authentication in the UrlConnection because of which I am getting an authentication error. Please help me out.

Rohit
  • 3
  • 3
  • 1
    `curl` by default will do HTTP Basic Authentication (you can see the added `Authorization: Basic ` header by using the `-v` flag). `HttpUrlConnection` does not provide any method to facilitate it, but I've linked another question that describes how to produce the header yourself if you need (using another HTTP client that helps with basic auth would probably be better) – Aaron Jun 14 '18 at 12:24
  • thanks @Aaron!! it worked. Also can you please suggest some nice HTTP client for working with curl – Rohit Jun 15 '18 at 05:56
  • I haven't got much experience on any Java HTTP Client, but [Apache's HttpClient](https://hc.apache.org/httpcomponents-client-ga/index.html) is a well known one and it does [handle basic auth](https://hc.apache.org/httpcomponents-client-4.5.x/httpclient/examples/org/apache/http/examples/client/ClientAuthentication.java) – Aaron Jun 15 '18 at 08:19
  • curl is already an http client library. – Perimosh Sep 12 '19 at 14:04

0 Answers0