Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
Servlet in AEM, body in POST request is lost

I created a custom tool in AEM and I wanted the custom tool to call a servlet(POST json).

The servlet was called but, request.getParameterMap() return empty map.

My servlet code is

@Component(
    service=Servlet.class,
    property={
        Constants.SERVICE_DESCRIPTION + "=Custom Servlet",
        "sling.servlet.methods=" + HttpConstants.METHOD_POST,
        "sling.servlet.paths=" + "/apps/myapp/customServlet"
    }
)
public class CustomServlet extends SlingAllMethodsServlet{
    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws IOException {
                String method = request.getMethod(); // method POST OK
                Map map = request.getParameterMap(); // return map but empty
                String name = request.getParameter("foo"); // also this return null
                
                response.setStatus(SlingHttpServletResponse.SC_OK);
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().print("{\"response message\" : \"" + foo + "\"}");
            }
}

and my JS code(loaded to AEM custom tool page as a client library)

window.onload = function() {
    var url = '/apps/myapp/customServlet';
    var button = document.getElementById('btnSubmit');

    button.addEventListener('click', event => {


        var foo = document.getElementById('foo').value;
        if (foo.length < 1){
            alert("input required!!");
        } else {
            var requestData = {};
            requestData.foo= foo;
            console.log(requestData); // it is ok
            postData(url,requestData).then(data => {console.log(data);}); // got {response message:null}
        }

    });
}

async function postData(url, data){
    const response = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',

        },
        body: JSON.stringify(data)
    });
    return response.json();
}

In addition, I deleted POST at filter methods in Adobe Granite CSRF Filter Config.

Do I need any other OSGi config or something wrong with my code to use Post Servlet in AEM?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

To get payload from request body, you can use request.getReader()

For example:

String body = IOUtils.toString(request.getReader()); //apache commons io

Or use some json mapper to immediately get your java object

YourObject yourObject = new ObjectMapper().readValue(request.getReader(), YourObject.class); // jackson
about 4 years ago · Juan Pablo Isaza Report

0

I'm seeing

var requestData = {}; // creating an empty array

and you are posting this to the servlet.

I think you want to parse 'foo' from document.getElementById to the servlet.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!