AEM Post Servlet

As part of GET method, it is not allowed to send large amount of data as part of URL. There is a limitation to send maximum of 2048 characters as part of URL.

To resolved above issue, we have POST method servlet to send large amount of data as part of request body.

To support POST method, we are going to use SlingAllMethodsServlet class.

As part of below servlet implementation, we are going to consume the same project we created as part of this link.

Let’s create a servlet class extending SlingAllMethodsServlet class having doPost(SlingHttpServletRequest req, SlingHttpServletResponse resp) overridden method inside core sub module.

We can not call POST method from browser as it is not allowed. Browser can always make only GET request.

We will be using postman application to access POST servlet URL. Follow below steps for the same:

  1. Open postman chrome plugin and select method as POST option and provide servlet URL.

Select Body tab, raw as an option, provide data in the form JSON.

Provide user data in the form of JSON as shown below.

2. Select Type as Basic Authentication, provide username and password as admin.

3. Clicking on Send button will call an AEM servlet register on /bin/user/save path and return response as JSON.

4. Servlet will successfully get call on click on Send of button in postman.

In case POST Servlet not getting call and throwing 403

To resolve this issue follow below steps:

1. Open below URL and look for Apache Sling Referrer filter

http://localhost:4502/system/console/configMgr

Check the Allow Empty checkbox and remove POST option from filter methods.

2. On the same URL and search for Adobe Granite CSRF Filter usinf ctrl +f. Remove POST option from Filter Methods field and add an entry as part of exclude paths as /bin/user/save

Try again clicking on Postman Send button will call Servlet this time.

Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.

0