AEM 6.3: Error 500 while doing a POST request | Community
Skip to main content
Level 4
Nov 21, 2017 7:45 PM UTC
Solved

AEM 6.3: Error 500 while doing a POST request

I have created a POST servlet like below:

package com.aem.sites.servlets;

import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Modified;

import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.aem.sites.interfaces.SubscriptionConfiguration;

import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

@Component(

immediate = true,

service = Servlet.class,

configurationPid = "com.aem.sites.servlets.SubscriptionSevlet",

property = {

"sling.servlet.methods=POST",

"sling.servlet.selectors=newsletters",

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

"sling.servlet.extensions=html"

}

)

@Designate(ocd=SubscriptionConfiguration.class)

public class SubscriptionSevlet extends SlingAllMethodsServlet{

/**

*

*/

private static final long serialVersionUID = 1L;

/** The Constant logger. */

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Override

protected void doPost(final SlingHttpServletRequest req, final SlingHttpServletResponse res) throws IOException, ServletException {

logger.info("======================inside do post for subscription servlet========================================");

}

/**

* Activate.

*

* @param config the config

*/

@Activate

    @Modified

    protected void Activate(SubscriptionConfiguration config) {

logger.info("********************************inside subscription servlet*****************************************"+config.sampleProperty());

    }

}

and this is the configuration Interface

package com.aem.sites.interfaces;

import org.osgi.service.metatype.annotations.AttributeDefinition;

import org.osgi.service.metatype.annotations.AttributeType;

import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@ObjectClassDefinition(name="Subscription Configuration", description="Configuration interface for Subscription Servlet")

public @interface SubscriptionConfiguration {

@AttributeDefinition(

        name = "Sample property",

        description = "Sample property",

        type = AttributeType.STRING

    )

public String sampleProperty() default "sample test";

}

I am calling the servlet using resourceType and selector.The servlet is being called through a form:

<div id="login-box" style="padding-top:5%;padding-left:3%">

  <div class="left">

    <h1>Sign up</h1>

    <form method="POST" name="subscriptionForm" id="subscriptionForm" action="/content/aemsite/en/subscribe.newsletters.html">

    <input type="text" name="name" placeholder="Name" style="width:30%" />

    <input type="text" name="email" placeholder="E-mail" style="width:30%"/>

    <input type="submit" name="signup_submit" value="Sign me up" />

    </form>

  </div>

</div>

this is the JS being included through clientlibs

$(function() {

var formURL = $("#subscriptionForm").attr('action');

var method = $("#subscriptionForm").attr('method');

$("#subscriptionForm").submit(function(event) {

event.preventDefault();

$.ajax({

url:formURL,

data: $("#subscriptionForm").serialize(),

type: method,

success: function( data, textStatus, jQxhr ){

        $('#response pre').html( data );

    },

    error: function( jqXhr, textStatus, errorThrown ){

        console.log( errorThrown );

    }

});

});

});

The clientlibs that are being included as dependencies are given in the screenshot clientlibs.png below

The above is the error I am seeing on the chrome debuuger is  "Failed to load resource: the server responded with a status of 500 (Server Error)"

The screenshot is given below:

I have read that from AEM 6 onwards more security measures are being adopted for POST request and hence a CSRF token is being used but it is mostly handled if AEM's version of jQuery is used.

I have also used jcrresolver to check if the path is being resolved and here is the screenshot attached of that:

The servlet's status is also active.

I am not sure what is it that I am doing incorrectly. Any help is appreciated.

Thanks

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
smacdonald2008
smacdonald2008Accepted solution
Level 10
Nov 21, 2017 9:16 PM UTC

I just tested Mail List Component solution. It works nicely --

Data was posted to Servlet and put into the database

18 replies

joerghoh
Employee
Nov 22, 2017 10:05 PM UTC

I haven't played that much with the DS annotations yet, I don't know from the top of my head if the property syntax is correct. When I used the Felix SCR annotations it worked for me very well. You might give it a try.

Jörg

Translate
Level 4
Nov 22, 2017 10:01 PM UTC

I was looking to use OSGi DS annotations that's why I didn't use the @SlingServlet annotation. Do you think I should use the Felix SCR annotations for POSTrequest??

Translate
joerghoh
Employee
Nov 22, 2017 9:15 PM UTC

That's true.

Anyway, the SlingPostServlet was used. Maybe you can confirm this by validating your request through the servlet resolver page in the webconsole (localhost:4502/system/console/servletresolver).

Looking again at your code, the annotations seems to be wrong ...

@Component(

immediate = true,

service = Servlet.class,

configurationPid = "com.aem.sites.servlets.SubscriptionSevlet",

property = {

"sling.servlet.methods=POST",

"sling.servlet.selectors=newsletters",

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

"sling.servlet.extensions=html"

}

)

I would expect something like this

@Compont(immediate=true)

@Service

@Properties({

  @Property(name="sling.servlet.methods" value="POST"),

  @Property(name="sling.servlet.selectors" value="newsletters"),

  @Property(name="sling.servlet.resourceTypes" value="aemsite-project/components/structure/page"),

  @Property(name="sling.servlet.extensions" value="html")

})

(or the use of the @SlingServlet annotation).

Can you check that?

Jörg

Translate
Level 4
Nov 22, 2017 3:04 PM UTC

And if the resourceType wasn't correct it would have never worked for GET request either.

Translate
Level 4
Nov 22, 2017 3:03 PM UTC

Never meant I found a solution. Was just an observation. The /content/aemsite/en/subscribe is made using an editable template and the resource type is set to aemsite-project/components/structure/page.

Translate
joerghoh
Employee
Nov 22, 2017 7:27 AM UTC

Sorry, but hardcoding the path in the servlet is no solution! Don't do that for production code.

My assumption is that the page at /content/aemsite/en/subscribe does not have the resourcetype "aemsite-project/components/structure/page" as specified in the annotations of the servlet. Fix that and it will work!

Jörg

Translate
Level 4
Nov 22, 2017 5:25 AM UTC

Update - When I use the path instead of resourceType, it works fine.

Translate
Level 4
Nov 22, 2017 5:06 AM UTC

The path used in action is /content/aemsite/en/subscribe.newsletters.html while the page that has the POST form is http://localhost:4502/editor.html/content/aemsite/en/subscribe.html  Since this is an AJAX call I have selected the path in action to be /content/aemsite/en/subscribe.newsletters.html while 'newsletters' is the selector. Is there something I am doing wrong here ?

Translate
smacdonald2008
Level 10
Nov 21, 2017 9:40 PM UTC

Makes sense - i have never seen issue here.

Translate
joerghoh
Employee
Nov 21, 2017 9:38 PM UTC

How does your POST request look like? Can you post the exact adress you are posting to?

@Scott: I don't think that there is a bug in Sling; I have used this very functionality already a few times :-)

Jörg

Translate