Not Able to call Post Servlet in html form action tag | Community
Skip to main content
Level 2
October 12, 2023
Solved

Not Able to call Post Servlet in html form action tag

  • October 12, 2023
  • 2 replies
  • 2252 views

Hi,
I have created a custom search component and in that I had written a html file and for backend side I created a resource type servlet and services. The servlet is working fine, it is giving me the correct response while tested with postman. But when i used that servlet api in my custom component html page of form tag with action= servlet url and method=POST. Then I am getting an error 403 Forbidden with statement "Cannot serve request to /content/aem-site/us/en/component-basics/_jcr_content.geeks.xml in BundledScriptServlet (/libs/sling/servlet/errorhandler/default.jsp)".
Why this error is coming that I don't know. Then I figure out the Servlet Resolver where my servlet is working fine but not in html. Then I removed POST from the CSRF Filter then my servlet is working in the html. But why I have to remove the POST api method from the CSRF Filter, this is not proper solution. So can you please help me to find the solution for this.
I have attached videos for your reference and also for servlet backend code.

Component:
https://www.youtube.com/watch?v=m5z-htzR7ts&ab_channel=My%3FListeries 

Servlet : 

https://www.youtube.com/watch?v=OnqOffsIomM&ab_channel=My%3FListeries 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by sherinregi-1

Hi @brijesh-2203 

One of the ways will be to add the path to the excluded paths so filter will ignore validating this request and shouldn't return forbidden.

 

Another possibility is due to CSRF Token validation by AEM. If you find the specific error in logs please try to add the default granite.jquery in your custom component and try adding csrf.standalone dependency

 

https://experienceleague.adobe.com/docs/experience-manager-65/developing/introduction/csrf-protection.html?lang=en

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/post-servlet-giving-403-forbidden-error-during-post-ajax-call/m-p/449889

 

 

2 replies

A_H_M_Imrul
Community Advisor
Community Advisor
October 12, 2023

@brijesh-2203 

I would suggest to go for sling.servlet.paths instead of sling.servlet.resourceTypes specially when you doing a POST. 

Reference: https://medium.com/@toimrank/aem-post-servlet-e0b07ecc63ac

 

Level 2
October 13, 2023

@a_h_m_imrul 
I also tried with sling.servlet.paths but facing the same issue same as sling.servlet.resourceTypes.

sherinregi-1
Community Advisor
sherinregi-1Community AdvisorAccepted solution
Community Advisor
October 12, 2023

Hi @brijesh-2203 

One of the ways will be to add the path to the excluded paths so filter will ignore validating this request and shouldn't return forbidden.

 

Another possibility is due to CSRF Token validation by AEM. If you find the specific error in logs please try to add the default granite.jquery in your custom component and try adding csrf.standalone dependency

 

https://experienceleague.adobe.com/docs/experience-manager-65/developing/introduction/csrf-protection.html?lang=en

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/post-servlet-giving-403-forbidden-error-during-post-ajax-call/m-p/449889

 

 

Level 2
October 13, 2023

Hi @sherinregi-1 ,
I already added for my resourceType servlet in excluded paths but it is not working. But now I converted to the path Type servlet then excluded the serlvet path then it is working fine.

 

Thank you @sherinregi-1 


But can you please let me know why it is not worked with resourceType servlet but worked worked with path type servlet.

Level 2
October 13, 2023

Hi @sherinregi-1

By adding dependency of csrf.standalone and granite.jquery, It is working for both resourceType and path Servlet.
For resourceType - granite.jquery only this

For pathType  -  granite.jquery or csrf.standalone


Thank you