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
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Recommended way Sling Servlet Post method to send json raw data

bangar50
Level 7
Level 7

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
Arun_Patidar
Correct answer by
Community Advisor
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

View solution in original post

5 Replies
Shashi_Mulugu
Community Advisor
Community Advisor

@bangar50 can you share some more details such as

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

Hi @Shashi_Mulugu ,

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

Regards,

Sanjay

SivaPrasadReddyS
Level 7
Level 7

@bangar50 

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. 

Arun_Patidar
Correct answer by
Community Advisor
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

View solution in original post

MohitKumarK
Level 4
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!