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

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
jebs89
Level 2

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

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
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);

    }

}

Avatar

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
jebs89
Level 2

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?

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
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  ?

Avatar

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
jebs89
Level 2

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?

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,318 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

23-08-2018

Hi,

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

Avatar

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile

Avatar
Validate 25
Level 10
smacdonald2008
Level 10

Likes

1,409 likes

Total Posts

12,671 posts

Correct reply

2,278 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give back 900
Give back 600
View profile
smacdonald2008
Level 10

23-08-2018

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

Avatar

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
jebs89
Level 2

23-08-2018

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

Avatar

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
jebs89
Level 2

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

}

}

}

Avatar

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile

Avatar
Boost 1
Level 2
jebs89
Level 2

Like

1 like

Total Posts

8 posts

Correct reply

0 solutions
Top badges earned
Boost 1
View profile
jebs89
Level 2

23-08-2018

This is the printed logs:

inside MerlionWebhookServlet doGet

inside MerlionWebhookServlet dopost

Method: GET

jsonRequestText :