Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Sling servlet with PUT method

Avatar

Level 2

Hello Everyone,

 

I was trying to create a sling servlet with doPut method override, please find code snippet below

@SlingServlet(paths = {"/bin/mysite/ravi"}, 
methods = {HttpConstants.METHOD_GET, HttpConstants.METHOD_PUT}) 
public class MyServlet extends SlingAllMethodsServlet { 
@Override 
protected void doPut(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws IOException { 
response.getWriter().write("My Servlet called"); 
response.getWriter().close(); 
} 
}

When I tried accessing from Postman, by access below URL, http://localhost:4502/bin/mysite/ravi with PUT method and Basic Auth. But it never calls doPut method from servlet. Any help is appreciated and thanks in advance. 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@rgravisankar,

You might not have your AEM configured to allow PUT methods, because all PUT methods are being filtered. From your local AEM, you need to configure the Apache Sling Referrer Filter.
In OSGI configurations (http://localhost:4502/system/console/configMgr), locate “Apache Sling Referrer Filter”. Remove the PUT method from filters property.

If you would like to test your Servlet via POSTMAN, you can follow this tutorial here -> https://sourcedcode.com/blog/aem/how-to-make-simple-http-post-methods-in-aem-with-a-http-rest-client

 

 

View solution in original post

2 Replies

Avatar

Community Advisor

Hi @rgravisankar, did you try to use http://localhost:4502/system/console/requests to check what is consuming your PUT request, you are sending from Postman? Could you share the output of recent request - this will be useful to figure out what could be the root cause of the issue. I would assume that your servlet has been successfully registered as an OSGi component, right?

Avatar

Correct answer by
Community Advisor

@rgravisankar,

You might not have your AEM configured to allow PUT methods, because all PUT methods are being filtered. From your local AEM, you need to configure the Apache Sling Referrer Filter.
In OSGI configurations (http://localhost:4502/system/console/configMgr), locate “Apache Sling Referrer Filter”. Remove the PUT method from filters property.

If you would like to test your Servlet via POSTMAN, you can follow this tutorial here -> https://sourcedcode.com/blog/aem/how-to-make-simple-http-post-methods-in-aem-with-a-http-rest-client