Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

309
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda