Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

POST Servlet using Page resource type - Unable to pass form data

santhoshsrg
Level 4
Level 4

Hi everyone,

 

I wanted to create a POST servlet using Resource Type and pass form data. I will be calling this servlet from Component X which is an embedded component. I created a POST servlet using Page component's resourceType. It works fine when form data is not passed. But when form data is passed, it is throwing some exceptions like in the screenshot below. The data i am trying to pass is "img" as in screenshot.

 

I also tried to create POST servlet using Component Y's resourceType and i was able to pass form data here. Since I am using an embedded component, Servlet using Page resource type will be apt for this.

 

Any help would be great. 

 

Screenshot 2020-05-14 at 7.31.47 PM.pngScreenshot 2020-05-14 at 7.31.02 PM.png

1 Accepted Solution
santhoshsrg
Correct answer by
Level 4
Level 4

I did a small mistake. I was using URL as pagePath.save.html in ajax to hit my servlet. 

Then i changed URL to pagePath_jcr_content.save.html. This worked.

 

Thank you all for you inputs!

View solution in original post

0 Replies
Ankur_Khare
Community Advisor
Community Advisor

Nothing to do with page or component resource type.

Where are you trying to post this data to?

 

santhoshsrg
Level 4
Level 4

I am just using POST servlet to upload image to asset. But I am not able to pass any data. When i didn't pass data, servlet is working fine.

Ankur_Khare
Community Advisor
Community Advisor
Then its visible with the error that data which you are trying to add to a node is invalid. Check any asset which is already in dam and try ti post same data and see how it behaves.
santhoshsrg
Level 4
Level 4
Actually I am uploading asset as "filename_timestamp.png". So each time, new asset should be uploaded to DAM
Ankur_Khare
Community Advisor
Community Advisor
Try to upload the asset manually with the same name (with timestamp and all)and data and see are you able to upload
santhoshsrg
Level 4
Level 4

Sorry for the misunderstanding. Servlet functionality is working fine when component resource type is used. The problem is only with servlet with page resource type

Ankur_Khare
Community Advisor
Community Advisor
If possible can u add your code here so that i can try at my end?
Shashi_Mulugu
Community Advisor
Community Advisor

@santhoshsrg Can you paste some code snippet how are you trying to read the payload from the request. Also you said, you want to post formdata but in your question you commented as posting image. Can you please confirm what exactly you are trying to post and how are you reading payload from request.

 

This will help us to help you.

santhoshsrg
Level 4
Level 4

Hi,

 

I am passing Image in base64 format to servlet as form data and in servlet i am uploading image to asset. The issue occurs before servlet code ran. 

 

JS Code:

let data = {img: img};
$.ajax({
type: "POST",
url: url,
data,
success: function (response) {
displayModal(SUCCESS_MSG);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
    displayModal(ERROR_MSG);
}
}).done(function() {
    $(".logo-mobile img").show();
});

 

Servlet code: 

@component(immediate = true, service = Servlet.class,

property={

        "sling.servlet.methods=" + HttpConstants.METHOD_POST,

        "sling.servlet.resourceTypes="+ "project/components/structure/page",

        "sling.servlet.selectors=" + "save",

        "sling.servlet.extensions=" + "html"

})

public class SaveScreenshotServlet extends SlingAllMethodsServlet  {

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)

throws ServletException, IOException {

    String img = request.getParameter(IMG_PARAM);

}

santhoshsrg
Correct answer by
Level 4
Level 4

I did a small mistake. I was using URL as pagePath.save.html in ajax to hit my servlet. 

Then i changed URL to pagePath_jcr_content.save.html. This worked.

 

Thank you all for you inputs!

View solution in original post