How to invoke 3rd part REST API using AEM sling servlet | Community
Skip to main content
Adobe Employee
July 15, 2022
Solved

How to invoke 3rd part REST API using AEM sling servlet

  • July 15, 2022
  • 2 replies
  • 2631 views

Hi,

 

I have a requirement to invoke the client service using rest api. 3 different post is used to insert a data, read the data and delete the data and the params accordingly. I need to get the response in json format when we are reading the data and a success msg for insert and delete. How to invoke the client service and get the response using the url, params, headers(api key)?

 

CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(siteService.getAwsUrl());
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("userid", userId));
params.add(new BasicNameValuePair(URL, bookmarkUrl));
params.add(new BasicNameValuePair(ACTION, action));
params.add(new BasicNameValuePair(SITECODE, siteCode));
params.add(new BasicNameValuePair(CATEGORY, category));
params.add(new BasicNameValuePair(TITLE, title));
httpPost.setEntity(new UrlEncodedFormEntity(params));
List<Header> headerList = siteService.getHeaders();
Header[] headers = headerList.toArray(new Header[0]);
httpPost.setHeaders(headers);
CloseableHttpResponse jsonResponse = client.execute(httpPost);

the response is giving status code as 400. 

 

Thank you

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 Sachin_Arora_

You can write HTTP connection logic in same way as we do in JAVA. If your logic is working fine in JAVA class, it work fine in AEM servlet as well.

400 Error response code indicates client error. Please try same in Postman first and try to build same request in JAVA class using APIs. Once it looks fine you can create a servlet in AEM.


Document to write servlet : aem.redquark.org/2018/10/day-05-working-with-sling-servlets-in_10.html

 

Sample code to send response in JSON in servlet :

Employee employee = new Employee(1, "Karan", "IT", 5000);
String employeeJsonString = this.gson.toJson(employee);
PrintWriter out = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(employeeJsonString);
out.flush();   

2 replies

Sachin_Arora_
Community Advisor
Sachin_Arora_Community AdvisorAccepted solution
Community Advisor
July 15, 2022

You can write HTTP connection logic in same way as we do in JAVA. If your logic is working fine in JAVA class, it work fine in AEM servlet as well.

400 Error response code indicates client error. Please try same in Postman first and try to build same request in JAVA class using APIs. Once it looks fine you can create a servlet in AEM.


Document to write servlet : aem.redquark.org/2018/10/day-05-working-with-sling-servlets-in_10.html

 

Sample code to send response in JSON in servlet :

Employee employee = new Employee(1, "Karan", "IT", 5000);
String employeeJsonString = this.gson.toJson(employee);
PrintWriter out = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(employeeJsonString);
out.flush();