Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

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

Avatar

Level 2

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?

15 Replies

Avatar

Community Advisor

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);

    }

}



Arun Patidar

Avatar

Level 2

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?

Avatar

Community Advisor

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  ?



Arun Patidar

Avatar

Level 2

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?

Avatar

Community Advisor

Hi,

Can you share your servlet code skeleton(remove business logic) ?



Arun Patidar

Avatar

Level 10

Are you logging into AEM itself or building a login component to a specific site built via AEM?

Avatar

Level 2

I am logging into AEM itself and hitting the resource located at 'resource'

Avatar

Level 2

@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();

}

}

}

Avatar

Level 2

This is the printed logs:

inside MerlionWebhookServlet doGet

inside MerlionWebhookServlet dopost

Method: GET

jsonRequestText :

Avatar

Community Advisor

Hi,

If you directly hit the URL in the browser, it will. be treated as GET request. To make POST request either submit form  or call url via ajax using post method.

Screen Shot 2018-08-24 at 9.06.16 AM.png

Screen Shot 2018-08-24 at 9.19.05 AM.png



Arun Patidar

Avatar

Level 2

I use POSTMAN to send a POST call attaching the request body in it.

Avatar

Community Advisor

Hi,

In above screenshot you can see, I made the post request for same code using AJAX. If I hit same page directly with browser I am getting GET.

So the real issue is the way you are making call to your servlet.



Arun Patidar

Avatar

Community Advisor

My perspective you should be invoking the servlet URL directly with POST method and set the authentication header as part of the request.

e.g Authorization Basic YWRtaW46YWRtaW4=

Encode the following string username:password(e.g admin:admin)

Regards

Albin I

www.albinsblog.com

Avatar

Level 2

i use a URL http://admin:admin@localhost:4502/ecat/apps/cat/injectorwebhook. This takes me to the servlet and is treated as a POST and all is good.


Logs:

inside MerlionWebhookServlet dopost

Method: POST

jsonRequestText : {

  "fragments": {

    "DDD001017_id06": {

      "targetLangs": ["es_XC", "zh_XC"]

    },

    "EEE001018_id01": {

      "targetLangs": ["fr_FR"]

    }

  }

}

But due to dependent system behavior i cannot use this URL. What is the difference in behavior between these two URLs?

Avatar

Employee Advisor

Please find this request in the server logs; in the request log you should find this request and it will clearly identify what method was used.

Jörg