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 | Community
Skip to main content
jebs89
Level 2
August 23, 2018

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

  • August 23, 2018
  • 4 replies
  • 5274 views

http://localhost:4502/ecat/j_security_check?j_username=abc&j_password=abc&resource=/ecat/apps/cat/injectorwebhook 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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

arunpatidar
Community Advisor
Community Advisor
August 23, 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);

    }

}

Arun Patidar
jebs89
jebs89Author
Level 2
August 23, 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?

arunpatidar
Community Advisor
Community Advisor
August 23, 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  ?

Arun Patidar
arunpatidar
Community Advisor
Community Advisor
August 23, 2018

Hi,

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

Arun Patidar
jebs89
jebs89Author
Level 2
August 24, 2018

This is the printed logs:

inside MerlionWebhookServlet doGet

inside MerlionWebhookServlet dopost

Method: GET

jsonRequestText :

Community Advisor
August 24, 2018

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.


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

jebs89
jebs89Author
Level 2
August 24, 2018

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?

joerghoh
Adobe Employee
Adobe Employee
August 24, 2018

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