Expand my Community achievements bar.

SOLVED

How can I fetch the output in json format from DAM parent node to certain level

Avatar

Employee

I have requirement to get all the assets and its properties in .json format at certain level by URl as /content/dam/abc/a.1.json

I am able to get data using httpclient library by get call ,Is there any other way to fetch the data in json format as I don't want to pass username and password in get call

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ashishg1547773 

 

You can also use SlingRequestProcessor Library. It requires AEM system user to make a request similar to HTTP request. The system user must have access to path where you are making a request.

 

You can take a look at the sample code:

 

        String assetUrl = assetPath + ".1.json";
        HttpServletRequest request = requestResponseFactory.createRequest("GET", assetUrl);
        request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, WCMMode.DISABLED);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpServletResponse response = requestResponseFactory.createResponse(out);
        try {
            requestProcessor.processRequest(request, response, resourceResolver);
            String jsonString = out.toString(HQConstants.UTF_8_ENCODING);
        } catch (ServletException | IOException e) {
        } 

 

Here, you need resourceResolver which must be of system user with right access rights. The above makes same request as HTTP request and as your requirment, does not need credentials.

 

Hope it helps!

Thanks!

Nupur 

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @ashishg1547773 

 

You can also use SlingRequestProcessor Library. It requires AEM system user to make a request similar to HTTP request. The system user must have access to path where you are making a request.

 

You can take a look at the sample code:

 

        String assetUrl = assetPath + ".1.json";
        HttpServletRequest request = requestResponseFactory.createRequest("GET", assetUrl);
        request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, WCMMode.DISABLED);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HttpServletResponse response = requestResponseFactory.createResponse(out);
        try {
            requestProcessor.processRequest(request, response, resourceResolver);
            String jsonString = out.toString(HQConstants.UTF_8_ENCODING);
        } catch (ServletException | IOException e) {
        } 

 

Here, you need resourceResolver which must be of system user with right access rights. The above makes same request as HTTP request and as your requirment, does not need credentials.

 

Hope it helps!

Thanks!

Nupur