I am also using the same code for similar set of requirements and i was stuck on this error.
I have solved it.
PFB the steps. Either of them/All might work for you as well.
I have however used the annotations differently.
@Component(
immediate = true, metatype = true, label = "Engage Now Form",
description = "Engage Now Form")
@Service
@Properties({
@Property(name = "sling.servlet.paths", value = {"/bin/myServlet"}),
@Property(name = "sling.servlet.methods", value = {"POST"})
})
I have given permission to my /bin/myServlet in the org.apache.sling.servlets.resolver.SlingServletResolver.
Used the poster plugin - set the referer in the header to some html existing in your site heirarchy other wise it will give 403 forbidden error. Also mention the parameters to send and go to content to send and say body from parameters. The below text box shall be populated. Now try posting the data.
For the CQ specific part :- i have implemented an AJAX based solution with forms where i send the POST call using AJAX on submit click. I also have a custom action in place with a forward.jsp containing the following code.
final ValueMap properties = ResourceUtil.getValueMap(resource);
String path = properties.get("customRedirectPath", "");
//path = /bin/myServlet
FormsHelper.setForwardPath(slingRequest, path);
FormsHelper.setRedirectToReferrer(request, true);
My AJAX call is :-
argsObject = {
oServiceUrl: "/bin/myServlet",
oAjaxFormat: "html",
oDataForAjax: $thisForm.serialize(),
oAjaxCommMethod: "POST",
oSuccessCallback: onSubmitSuccessCallback,
oErrorCallback: onSubmitErrorCallback
}
I am able to execute my servlet once this POST call is executed and i recieve a valid response as desired.
Let me know if these steps help.