Reputation: 163
I created a custom tool in AEM and I wanted the custom tool to call a servlet(POST json).
The servlet was called but, request.getParameterMap() return empty map.
My servlet code is
@Component(
service=Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Custom Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.paths=" + "/apps/myapp/customServlet"
}
)
public class CustomServlet extends SlingAllMethodsServlet{
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws IOException {
String method = request.getMethod(); // method POST OK
Map map = request.getParameterMap(); // return map but empty
String name = request.getParameter("foo"); // also this return null
response.setStatus(SlingHttpServletResponse.SC_OK);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().print("{\"response message\" : \"" + foo + "\"}");
}
}
and my JS code(loaded to AEM custom tool page as a client library)
window.onload = function() {
var url = '/apps/myapp/customServlet';
var button = document.getElementById('btnSubmit');
button.addEventListener('click', event => {
var foo = document.getElementById('foo').value;
if (foo.length < 1){
alert("input required!!");
} else {
var requestData = {};
requestData.foo= foo;
console.log(requestData); // it is ok
postData(url,requestData).then(data => {console.log(data);}); // got {response message:null}
}
});
}
async function postData(url, data){
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
return response.json();
}
In addition, I deleted POST at filter methods in Adobe Granite CSRF Filter Config.
Do I need any other OSGi config or something wrong with my code to use Post Servlet in AEM?
Upvotes: 0
Views: 2717
Reputation: 301
I'm seeing
var requestData = {}; // creating an empty array
and you are posting this to the servlet.
I think you want to parse 'foo' from document.getElementById
to the servlet.
Upvotes: 0
Reputation: 310
To get payload from request body, you can use request.getReader()
For example:
String body = IOUtils.toString(request.getReader()); //apache commons io
Or use some json mapper to immediately get your java object
YourObject yourObject = new ObjectMapper().readValue(request.getReader(), YourObject.class); // jackson
Upvotes: 1