Recommended way Sling Servlet Post method to send json raw data | Community
Skip to main content
Sanjay_Bangar
New Participant
Oct 28, 2021 4:43 PM UTC
Solved

Recommended way Sling Servlet Post method to send json raw data

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.
arunpatidar
arunpatidarAccepted solution
New Participant
Oct 30, 2021 6:26 PM UTC

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

5 replies

MohitKumarK
Level 3
Nov 6, 2021 9:58 AM UTC

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!

Translate
arunpatidar
arunpatidarAccepted solution
New Participant
Oct 30, 2021 6:26 PM UTC

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

Translate
Arun Patidar
Shashi_Mulugu
New Participant
Oct 28, 2021 6:20 PM UTC

@sanjay_bangar can you share some more details such as

  • AEM version 
  • Is this requirement on author or publisher 
  • Are they behind dispatcher Etc..
Translate
Sanjay_Bangar
New Participant
Oct 29, 2021 2:04 AM UTC

Hi @shashi_mulugu ,

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

Regards,

Sanjay

Translate
Siva_Sogalapalli
New Participant
Oct 29, 2021 11:29 AM UTC

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

Translate