How to call 3rd part REST API from AEM (in server side code) | Community
Skip to main content
New Participant
April 3, 2021
Solved

How to call 3rd part REST API from AEM (in server side code)

  • April 3, 2021
  • 3 replies
  • 14387 views

There are some old posts which discuss this, but they point to broken links.

 

e.g. https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/conclusion-consuming-rest-api-or-web-service-from-aem/qaq-p/244300

 

points to https://helpx.adobe.com/experience-manager/using/restful-services.html which gives 404.

 

https://github.com/brandonmaynard/aem-restful-api has a REST client, but no documentation.

 

https://aem4beginner.blogspot.com/invoke-rest-services-in-aem-right-way uses OpenFeign, which is not a library I have come across, and there is not much information on how to do this with AEM.

 

If this was a normal Java app, we could use jersy, Jax-rs or Spring RestTemplate. I am hoping AEM has one of the standard libraries built in.

 

I am guessing HttpUrlConnection might be included in AEM, in which case we could write our own REST handler on top of this if all else fails.

 

What does everyone use to make API calls from AEM?

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Asutosh_Jena_

Hi @tb3dock 

You can use 

CloseableHttpClient client = HttpClients.createDefault()

and execute the REST APIs on AEM side.

 

For this you will need to add the below dependency and rest all will work like butter 🙂

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>

A sample method that I personally use for making all the API requests from AEM:

 

public APIResponse makePostWSCall(String endPointURL, String requestBody, Map<String, String> requestParams, Map<String, String> headers) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
final long startTime = System.nanoTime();
endPointURL = makeEndPointURL(endPointURL, requestParams);

final String endPoint = StringEscapeUtils.escapeJava(endPointURL);
log.debug("RestServiceImpl :: makePostWSCall :: EndPoint URL --> {}", endPoint);

HttpPost httpPost = new HttpPost(endPointURL);
if (null != headers && !headers.isEmpty()) {
for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
httpPost.addHeader(headerEntry.getKey(), headerEntry.getValue());
}
}

requestBody = WebServiceUtil.getPrettyJsonString(requestBody);
log.debug("RestServiceImpl :: makePostWSCall :: Request Body --> {}", requestBody);
if (StringUtils.isNotEmpty(requestBody)) {
StringEntity stringEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
}

APIResponse apiResponse = new APIResponse();
CloseableHttpResponse response = client.execute(httpPost);
final int statusCode = response.getStatusLine().getStatusCode();
apiResponse.setStatusCode(statusCode);
log.info("RestServiceImpl :: makePostWSCall :: Response Status Code --> {}", statusCode);

getResponse(endPointURL, startTime, apiResponse, response, "RestServiceImpl :: WSCall :: Response --> {}");

return apiResponse;
} catch (Exception e) {
log.error("RestServiceImpl :: makePostWSCall :: Exception --> ", e);
}
return null;
}

Hope this will be helpful!

Thanks!

3 replies

Rohit_Utreja
New Participant
April 5, 2021

Hi @tb3dock,

 

You can refer to below URL, it has a detailed implementation.

 

https://medium.com/@codeandtheory/invoke-rest-services-in-aem-the-right-way-c5fb0af43afe

 

Singaiah_Chintalapudi
New Participant
April 3, 2021

I would use Apache Http Client.

https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient

 

The JAX-RS comes with additional dependencies (this download additional javax dependencies) which might have a conflict with Sling dependencies.

 

Thanks,

Singaiah

Asutosh_Jena_
Asutosh_Jena_Accepted solution
New Participant
April 3, 2021

Hi @tb3dock 

You can use 

CloseableHttpClient client = HttpClients.createDefault()

and execute the REST APIs on AEM side.

 

For this you will need to add the below dependency and rest all will work like butter 🙂

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>

A sample method that I personally use for making all the API requests from AEM:

 

public APIResponse makePostWSCall(String endPointURL, String requestBody, Map<String, String> requestParams, Map<String, String> headers) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
final long startTime = System.nanoTime();
endPointURL = makeEndPointURL(endPointURL, requestParams);

final String endPoint = StringEscapeUtils.escapeJava(endPointURL);
log.debug("RestServiceImpl :: makePostWSCall :: EndPoint URL --> {}", endPoint);

HttpPost httpPost = new HttpPost(endPointURL);
if (null != headers && !headers.isEmpty()) {
for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
httpPost.addHeader(headerEntry.getKey(), headerEntry.getValue());
}
}

requestBody = WebServiceUtil.getPrettyJsonString(requestBody);
log.debug("RestServiceImpl :: makePostWSCall :: Request Body --> {}", requestBody);
if (StringUtils.isNotEmpty(requestBody)) {
StringEntity stringEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
}

APIResponse apiResponse = new APIResponse();
CloseableHttpResponse response = client.execute(httpPost);
final int statusCode = response.getStatusLine().getStatusCode();
apiResponse.setStatusCode(statusCode);
log.info("RestServiceImpl :: makePostWSCall :: Response Status Code --> {}", statusCode);

getResponse(endPointURL, startTime, apiResponse, response, "RestServiceImpl :: WSCall :: Response --> {}");

return apiResponse;
} catch (Exception e) {
log.error("RestServiceImpl :: makePostWSCall :: Exception --> ", e);
}
return null;
}

Hope this will be helpful!

Thanks!