jebs89
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?
Arun_Patidar
MVP
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);
}
}
jebs89
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?
Arun_Patidar
MVP
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 ?
jebs89
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?
Arun_Patidar
MVP
Arun_Patidar
MVP
23-08-2018
Hi,
Can you share your servlet code skeleton(remove business logic) ?
smacdonald2008
smacdonald2008
23-08-2018
Are you logging into AEM itself or building a login component to a specific site built via AEM?
jebs89
jebs89
23-08-2018
I am logging into AEM itself and hitting the resource located at 'resource'
jebs89
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();
}
}
}
jebs89
jebs89
23-08-2018
This is the printed logs:
inside MerlionWebhookServlet doGet
inside MerlionWebhookServlet dopost
Method: GET
jsonRequestText :