Sling servlet with PUT method | Community
Skip to main content
rgravisankar
Level 2
November 24, 2021
Solved

Sling servlet with PUT method

  • November 24, 2021
  • 2 replies
  • 2084 views

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. 

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 BrianKasingli

@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

 

 

2 replies

lukasz-m
Community Advisor
Community Advisor
November 24, 2021

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?

BrianKasingli
Community Advisor and Adobe Champion
BrianKasingliCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
November 25, 2021

@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