Expand my Community achievements bar.

SOLVED

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

Avatar

Level 5

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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();   

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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();