Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM Server side Javascript to call third party API

Kam-nyc
Level 2
Level 2

Due to security and cross domain concerns, the AEM component has to call third party API using server side request.

So, client side "http get" to AEM sever, AEM server make request post to the third part API, once server side request successfully, it would pass the data back to client.

 

Do we have any examples about this either Java or Server side JavaScript would be helpful, thanks.

1 Accepted Solution
Manjunath_K
Correct answer by
Community Advisor
Community Advisor

Hi @Kam-nyc ,

In backed you can call third party API as mentioned below, you can pass the response from API back to client side & use that response data as per your requirement.

 

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 
connectionManager.getParams().setConnectionTimeout(); //set this value
connectionManager.getParams().setSoTimeout(); //set this value
connectionManager.getParams().setMaxTotalConnections(); //set this value
connectionManager.getParams().setDefaultMaxConnectionsPerHost(); //set this value

HttpClient httpClient = new HttpClient(connectionManager);
PostMethod postMethod = new PostMethod("API endpoint");

try {
postMethod.setRequestEntity(new StringRequestEntity("data to send in request","contentType","charset")); //ex: new StringRequestEntity(inputData,"application/xml","UTF-8")
int statusCode = httpClient.executeMethod(postMethod);
if(statusCode == 200){
String responseData = postMethod.getResponseBodyAsString(); //send this responseData to front end
}
} catch (UnsupportedEncodingException exception) {
log.error("UnsupportedEncodingException occurred",exception);
} catch (IOException exception) {
log.error("IOException occurred",exception);
}

 

Hope this helps!

View solution in original post

6 Replies
santhosh_kumark
Community Advisor
Community Advisor

Hi @Kam-nyc ,

 

You can try it using Java on backend to invoke the 3rd party - as shown here where we invoke a Google Service and display the results in front end component -- Creating an Adobe Experience Manager 6.3 HTL Component that displays data from a Restful Web Service

 

Hope this would be helpful.

 

Thanks & Regards,

Santosh

Manjunath_K
Correct answer by
Community Advisor
Community Advisor

Hi @Kam-nyc ,

In backed you can call third party API as mentioned below, you can pass the response from API back to client side & use that response data as per your requirement.

 

MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 
connectionManager.getParams().setConnectionTimeout(); //set this value
connectionManager.getParams().setSoTimeout(); //set this value
connectionManager.getParams().setMaxTotalConnections(); //set this value
connectionManager.getParams().setDefaultMaxConnectionsPerHost(); //set this value

HttpClient httpClient = new HttpClient(connectionManager);
PostMethod postMethod = new PostMethod("API endpoint");

try {
postMethod.setRequestEntity(new StringRequestEntity("data to send in request","contentType","charset")); //ex: new StringRequestEntity(inputData,"application/xml","UTF-8")
int statusCode = httpClient.executeMethod(postMethod);
if(statusCode == 200){
String responseData = postMethod.getResponseBodyAsString(); //send this responseData to front end
}
} catch (UnsupportedEncodingException exception) {
log.error("UnsupportedEncodingException occurred",exception);
} catch (IOException exception) {
log.error("IOException occurred",exception);
}

 

Hope this helps!

View solution in original post

Kam-nyc
Level 2
Level 2

Thanks Manjunathh,

 

I am new to Java and AEM, I got getMultiThreadedConf() method is undefined,

and httpClient.executeMethod method is undefined of the type of HttpClient.

Can you provide the method code if you dont mind?

Manjunath_K
Community Advisor
Community Advisor

@Kam-nyc  please check the above code updated with initializing & getting MultiThreadedHttpConnectionManager reference.

BrianKasingli
Community Advisor
Community Advisor

It's not a problem, you can make an HTTP request to an external micro-service securely via JavaScript without any problems. You just need to make sure your website's domain is whitelisted.

Here's an example using Javascript:

$("button").click(function(){
  $.post("https://api.adobe.com/profile",
  {
    name: "Bob",
    city: "Vancouver"
  },
  function(data, status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});