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?
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);
}
}
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?
Views
Replies
Total Likes
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 ?
Views
Replies
Total Likes
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?
Views
Replies
Total Likes
Hi,
Can you share your servlet code skeleton(remove business logic) ?
Views
Replies
Total Likes
Are you logging into AEM itself or building a login component to a specific site built via AEM?
Views
Replies
Total Likes
I am logging into AEM itself and hitting the resource located at 'resource'
Views
Replies
Total Likes
@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();
}
}
}
Views
Replies
Total Likes
This is the printed logs:
inside MerlionWebhookServlet doGet
inside MerlionWebhookServlet dopost
Method: GET
jsonRequestText :
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
I use POSTMAN to send a POST call attaching the request body in it.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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?
Views
Replies
Total Likes
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
Views
Replies
Total Likes