Json object giving null value after sending json array to the sling Servlet | Community
Skip to main content
Sanjay_Bangar
Community Advisor
Community Advisor
November 2, 2021
Solved

Json object giving null value after sending json array to the sling Servlet

  • November 2, 2021
  • 2 replies
  • 4577 views

Hi All,

         I have created Sling Servlet and  I wanted to pass json raw data to the Slingservlet but it is showing null values.

please check anything that I missed.

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 MohitKumarK

Hi @sanjay_bangar ,

 

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”,

 

);

 

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!

 

 

2 replies

Kiran_Vedantam
Community Advisor
Community Advisor
November 2, 2021

Hi @sanjay_bangar 

 

Try 2nd the solution in the below link: https://stackoverflow.com/questions/33896136/read-json-message-from-http-post-request-in-java

 

Hope this helps.

 

Thanks,

Kiran Vedantam

MohitKumarK
MohitKumarKAccepted solution
Level 3
November 4, 2021

Hi @sanjay_bangar ,

 

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”,

 

);

 

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!