AEM always returns 200 status code.

janellic4

29-08-2019

Upon requesting non-existent pages, both aem author and publish instances return blank page with 200 status code. The default 404 error page is not showing.

Accepted Solutions (1)

Accepted Solutions (1)

berliant

Employee

03-09-2019

Do you have any custom filters that can possibly massage the response? You might want to stop your custom bundles in OSGi and use /system/console/requests to track recent requests.

Answers (33)

Answers (33)

janellic4

09-10-2019

Hi DariaSuleimanova,

Filter will work without this property. For my situation, I want to restrict my custom filter to certain servlet, so I use the sling.filter.pattern property.

@Component(service = Filter.class,

property = {

                    Constants.SERVICE_DESCRIPTION + "=My Filter",

             EngineConstants.SLING_FILTER_SCOPE + "=" + EngineConstants.FILTER_SCOPE_REQUEST,

                    EngineConstants.SLING_FILTER_PATTERN + "=" + "/my_path_pattern",

                   Constants.SERVICE_RANKING + ":Integer=-100"

           })

public class MyFilter implements Filter{

    private final Logger logger = LoggerFactory.getLogger(MyFilter.class);

    @Override

    public void doFilter(final ServletRequest request, final ServletResponse response,

final FilterChain filterChain) throws IOException, ServletException {

        final SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;

        String requestURI = slingRequest.getRequestURI();

         //code to do something

filterChain.doFilter(request, response);

    }

    @Override

    public void init(FilterConfig filterConfig) {

    }

    @Override

    public void destroy() {

    }

}

janellic4

04-09-2019

I figure out the problem of the custom filter, the pattern that restricts the filter to the target servlet has a wrong setting. Now I change it to the correct setting, the 404 Page works correctly now. Thank you for all your help!

antoniom5495929

04-09-2019

Hi,

I'm referring to your previous post:

6. Re: AEM always returns 200 status code.

yes, I created a 404.html and ResponseStatus.java under /apps/sling/servlet/errorhandler

In the 404.html, the code is as:

<sly data-sly-use.responseStatus="apps.sling.servlet.errorhandler.ResponseStatus">

    <sly data-sly-resource="/content/myproject/en/page-not-found.html"></sly>

</sly>

The code in ResponseStatus.java is:

package apps.sling.servlet.errorhandler;

import com.adobe.cq.sightly.WCMUsePojo;

public class ResponseStatus extends WCMUsePojo {

    @Override

    public void activate() throws Exception {

        getResponse().setStatus(404);

    }

}

If you have this implementation, you can try to change the setStatus in the ResponseStatus Pojo in order to check if you have an other different status code.

Let us know.

Thanks,

Antonio

antoniom5495929

04-09-2019

Hi,

Have you already tryied to change the status into the ResponseStatus.java?

Just to check if a different status code is returned let's try to do this:

public void activate() throws Exception {

        getResponse().setStatus(407);

    }

Let us know.

Thanks,

Antonio

DariaSuleimanov

09-10-2019

Thank you for your answer!

Can you please share your code?

I made Filter by @SlingFilter but didn't use sling.filter.pattern at all

Filter will not work without this property?

janellic4

09-10-2019

Hi DariaSuleimanova,

In my custom filter, there is a sling.filter.pattern property, but what I use is "pattern", so this filter cause the problem. After I changed it to "sling.filter.pattern", the 404 page shows correctly.

DariaSuleimanov

08-10-2019

Hi, janellic4!

I got the problem exactly like yours.

Can you tell what was wrong in your custom filter and how you fix it? How did you find any useful information by "/system/console/requests"? I can't see any changes of status-code here.

Also can you change status of response in custom filter when slice-model throws an exception?

Thank you,

Daria

janellic4

04-09-2019

Hi antoniom54959291

I tried changing the setStatus code in the errorhandler, the response still gets 200 for invalid url. I then stop the bundle that includes a custom filter (as suggested by @berliantberliantTherefore, it is the custom filter that causes the issue. I am still struggling on fixing the filter's problem.

janellic4

03-09-2019

Hi berliant,

In the custom filter, I actually add the logic to check the request URI, if it is not the path of the target servlet, the filter won't do anything (I use filterChain.doFilter(request, response) for this situation). Somehow I don't know why the response status code is always 200. If I put the filter logic inside the servlet and delete the filter, the 404 page works correctly.  But with this way, the servlet will have a very long code. I also implemented another filter to add header options to servlet response, however, this filter does not affect the status code.

janellic4

03-09-2019

Hi berliant,

Thank you for your reply. I have one custom filter that sets status code to 400 if the slinghttprequest does not satisfy certain condition. After I stopped the custom bundle, the 404 not found status code shows up correctly. However, this custom bundle is also needed in the project, is there any way to solve the issue instead of stopping the custom bundle?

Himanshu_Singhal

MVP

01-09-2019

Hi janellic4,

While saving the configuration, it displays the error but it saves the updated value. Try opening the value again and it should show updated "Response Buffer Size".
Even after increasing the response buffer size, do you still get the 200 status code?

Do you get any error as well in logs?

janellic4

30-08-2019

Hello cal-netsolutions,

Thank you for your suggestion. However, we have implemented a custom error handler. It works fine in Publish instance in another computer. It does not work in some computers and the stage. They have the same problem: invalid url always got 200 status code with blank page.

janellic4

30-08-2019

Hi, Umeshsondhi

Thank you for your reply. I have read the article you mentioned.  In localhost, I do not use a dispatcher. It seems that a blank page would always be returned by Publish for invalid url request. I don't know how this blank page is created.

Himanshu_Singhal

MVP

30-08-2019

That could be due to Response buffer size as well that you always get 200 instead of any other error code. If the rendered response buffer is more than defined one, then server send response back to browser in chunks and once it's sends a part of response, no matter if page works or not, if always return 200.

You can try increasing the buffer size by going into configuration:

Apache Felix Jetty Based Http Service

"Response Buffer Size"

Increase the size and if page throws error then it should get displayed as well.

janellic4

29-08-2019

Update for the curl command: I get 200 status for both the valid and invalid url request.

Response for requesting the valid url : http://localhost:4503/content/myproject/en/search-results.html

HTTP/1.1 200 OK

Date: Thu, 29 Aug 2019 21:45:38 GMT

X-Content-Type-Options: nosniff

Set-Cookie: cq-authoring-mode=TOUCH;Path=/;Expires=Thu, 05-Sep-2019 21:45:38 GMT

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Content-Type: text/html;charset=utf-8

Transfer-Encoding: chunked

Response for the invalid url: http://localhost:4503/content/myproject/en/no-page.html

HTTP/1.1 200 OK

Date: Thu, 29 Aug 2019 21:46:15 GMT

X-Content-Type-Options: nosniff

Set-Cookie: cq-authoring-mode=TOUCH;Path=/;Expires=Thu, 05-Sep-2019 21:46:15 GMT

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Content-Length: 0

janellic4

29-08-2019

I am not able to get the 404 response when running a curl command to request a non-existent page. Nothing shows up in the terminal, however, I am able to get the correct page for a valid url using curl.

For example, requesting http://localhost:4503/content/myproject/en/search-results shows the html page content, however, requesting http://localhost:4503/content/myproject/en/no-page did not get any response.

jbrar

Employee

29-08-2019

That does not look like a deafult behavior. AEM should give you a 404 for non existing nodes:

jbrar-macOS:~ jbrar$ curl -u admin:admin http://localhost:6333/content/geometrixx-outdoors/en/men/pants/fulani-noma -I

HTTP/1.1 404 Not Found

Date: Thu, 29 Aug 2019 21:08:49 GMT

X-Content-Type-Options: nosniff

Set-Cookie: cq-authoring-mode=TOUCH;Path=/;Expires=Thu, 05-Sep-2019 21:08:49 GMT;Max-Age=604800

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Content-Type: text/html;charset=utf-8

Transfer-Encoding: chunked

Can you check if you get the 404 response when running a curl command? If yes, the error handler implementation is wrong

janellic4

29-08-2019

yes, I created a 404.html and ResponseStatus.java under /apps/sling/servlet/errorhandler

In the 404.html, the code is as:

<sly data-sly-use.responseStatus="apps.sling.servlet.errorhandler.ResponseStatus">

    <sly data-sly-resource="/content/myproject/en/page-not-found.html"></sly>

</sly>

The code in ResponseStatus.java is:

package apps.sling.servlet.errorhandler;

import com.adobe.cq.sightly.WCMUsePojo;

public class ResponseStatus extends WCMUsePojo { 

    @Override

    public void activate() throws Exception {

        getResponse().setStatus(404);

    }

}