Sling Post servlet getting java.lang.IllegalStateException: Request Data has already been read | Community
Skip to main content
Level 6
April 3, 2023
Solved

Sling Post servlet getting java.lang.IllegalStateException: Request Data has already been read

  • April 3, 2023
  • 2 replies
  • 1373 views

In the post servlet binded with the resource type gives error 

java.lang.IllegalStateException: Request Data has already been read

while saving the dialog of component to which it is binded to.

 

InputStream stream = request.getInputStream();
This is the first line of doPost inside try block. How to resole this?

 

@9944223 protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { response.setStatus(HttpServletResponse.SC_OK); try { resourceResolver = request.getResourceResolver(); InputStream stream = request.getInputStream(); InputStreamReader reader = new InputStreamReader(stream); JsonElement json = JsonParser.parseReader(reader); .....logic..... } catch (Exception e) { log.error("Errors in Post Call {}", e); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
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 Sady_Rifat

If I am not wrong you are trying to get the request body to POJO class. In that case, you can use Gson,

Gson gson = new Gson(); MyPojo mypojo = gson.fromJson(request.getReader(), MyPojo.class);

 

2 replies

Shashi_Mulugu
Community Advisor
Community Advisor
April 3, 2023

@ronnie09 may I know what are you trying to achieve? I believe aem already does one sling post to save authored data to jcr repository on save of component dialog, that's the reason why you are not able to read that posted data one more time.

Sady_Rifat
Community Advisor
Sady_RifatCommunity AdvisorAccepted solution
Community Advisor
April 4, 2023

If I am not wrong you are trying to get the request body to POJO class. In that case, you can use Gson,

Gson gson = new Gson(); MyPojo mypojo = gson.fromJson(request.getReader(), MyPojo.class);