Recommended way Sling Servlet Post method to send json raw data | Community
Skip to main content
Sanjay_Bangar
Community Advisor
Community Advisor
October 28, 2021
Solved

Recommended way Sling Servlet Post method to send json raw data

  • October 28, 2021
  • 3 replies
  • 4018 views

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

    

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 arunpatidar

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

3 replies

Shashi_Mulugu
Community Advisor
Community Advisor
October 28, 2021

@sanjay_bangar can you share some more details such as

  • AEM version 
  • Is this requirement on author or publisher 
  • Are they behind dispatcher Etc..
Sanjay_Bangar
Community Advisor
Community Advisor
October 29, 2021

Hi @shashi_mulugu ,

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

Regards,

Sanjay

Siva_Sogalapalli
Community Advisor
Community Advisor
October 29, 2021

@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. 

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
October 30, 2021

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
MohitKumarK
Level 3
November 6, 2021

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!