Highlighted

While trying to login to AEM using POST having request body, the resource recognizes it as a GET and does not give the request body

jebs89

23-08-2018

http://localhost:4502/ecat/j_security_check?j_username=abc&j_password=abc&resource=/ecat/apps/cat/in... is the POST URL used to login to our application and land on the servlet path /ecat/apps/cat/injectorwebhook. It has a request body containing a json snippet. When i do a request.getMethod() in the servlet i get it as a GET and could not have a hold on the json from the request body. Why is it treated as a GET method? how to retrieve the body json from the request?

Replies

Highlighted

Arun_Patidar

MVP

23-08-2018

Hi,

To get request payload JOSN you need to read InputStream, e.g.

StringBuilder stringBuilder = new StringBuilder();

try(BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(request.getInputStream()))) {

    char[] charBuffer = new char[1024];

    int bytesRead;

    while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {

        stringBuilder.append(charBuffer, 0, bytesRead);

    }

}

Highlighted

jebs89

23-08-2018

Hi Arun. Thanks for your reply. I understand that we can use your code to extract request body from a POST call. But in my case my POST call is identified as a GET by my servlet. Now how can i get the request body?

Highlighted

Arun_Patidar

MVP

23-08-2018

Hi,

Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request.

The GET method means retrieve whatever information ([...]) is identified by the Request-URI.

Can't you make POST request to http://localhost:4502/ecat/j_security_check?j_username=abc&j_password=abc&resource=/ecat/a pps/cat/injectorwebhook  ?

Highlighted

jebs89

23-08-2018

As I said I do a POST call only, via POSTMAN. But when i capture the logs from my servlet, the request.getMethod() returns it as a GET. any idea why the method is not identified as a POST, instead a GET?

Highlighted

jebs89

23-08-2018

@Service({javax.servlet.Servlet.class,javax.servlet.ServletConfig.class,java.io.Serializable.class})

@Component

@Properties({

@org.apache.felix.scr.annotations.Property(name="sling.servlet.paths", value="/apps/cat/injectorwebhook"),

@org.apache.felix.scr.annotations.Property(name="service.vendor", value="abc Inc."),

@org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", value={"POST"}),

@org.apache.felix.scr.annotations.Property(name="sling.servlet.extensions", value={"json"})

}

)

public class MerlionWebhookServlet extends SlingAllMethodsServlet{

private static final long serialVersionUID = 2598426539166789515L;

private static final Logger log = LoggerFactory.getLogger(MerlionWebhookServlet.class);

protected void doGet(SlingHttpServletRequest req,

SlingHttpServletResponse resp) throws IOException {

System.out.println("inside MerlionWebhookServlet doGet");

log.info("MerlionWebhookServlet get");

doPost(req, resp);

}

protected void doPost(SlingHttpServletRequest request,

SlingHttpServletResponse response) throws IOException {

System.out.println("inside MerlionWebhookServlet dopost");

log.info("MerlionWebhookServlet post");

doJob(request, response);

}

private void doJob(SlingHttpServletRequest request,SlingHttpServletResponse response){

try {

System.out.println("Method : " + request.getMethod());

String jsonRequestText = IOUtils.toString(request.getReader());

System.out.println("jsonRequestText : " + jsonRequestText);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Highlighted

jebs89

23-08-2018

This is the printed logs:

inside MerlionWebhookServlet doGet

inside MerlionWebhookServlet dopost

Method: GET

jsonRequestText :