Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Recommended way Sling Servlet Post method to send json raw data

Avatar

Community Advisor

Hi,

    I have requirement to send to Json Raw data to the post sling Servlet .

Anyone know what will be the recommended way to achieve this.

Regards,

Sanjay

    

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

we are doing this but we are consuming within same application so for us there are no issues using POST sling servlet.

But if you are exposing json over post you have to care of a lot of security like enable filter to allow post

example https://sourcedcode.com/blog/aem/in-aem-what-is-the-apache-sling-referrer-filter-osgi-configuration



Arun Patidar

View solution in original post

5 Replies

Avatar

Community Advisor

@Sanjay_Bangar can you share some more details such as

  • AEM version 
  • Is this requirement on author or publisher 
  • Are they behind dispatcher Etc..

Avatar

Community Advisor

Hi @Shashi_Mulugu ,

  AEM version 6.5 and requirement on  both the author and publisher.

Regards,

Sanjay

Avatar

Community Advisor

@Sanjay_Bangar 

how is the Json data generated? is it dynamically generated or json stored in AEM?

Are you trying to call the servlet upon any form submission? 

 

you can try making ajax calls to the servlet and pass json data to the servlet post method. 

Avatar

Correct answer by
Community Advisor

Hi,

we are doing this but we are consuming within same application so for us there are no issues using POST sling servlet.

But if you are exposing json over post you have to care of a lot of security like enable filter to allow post

example https://sourcedcode.com/blog/aem/in-aem-what-is-the-apache-sling-referrer-filter-osgi-configuration



Arun Patidar

Avatar

Level 4

When doing a ajax call use something like below

 

var square = {};

square.length = 10;

square.width = 10;

$.ajax(

url: url,

data: JSON.stringify(square),

dataType: “json”

contentType: “application/json”,

Type: “POST”

);

 

while reading in servlet you can use like below:

 

String requestBody = request.getReader().lines().collect(Collectirs.joining());


you can use any Utils class to convert from string to json or java bean.

 

Thanks!