3
votes

Using this reference - https://github.com/ServiceStack/ServiceStack/wiki/Request-and-response-filters

Right now in my current implementation any incoming HEAD requests receives a 404 statuscode. I want to return 200 as the status code for HEAD requests, so the client will go further to make GET requests for actual output.

Obviously my code below is wrong in AppHost still returning a 404, pls correct me.

     this.RequestFilters.Add((httpReq, httpResp, requestDto) =>
        {

        });

        this.ResponseFilters.Add((req, res, dto) =>
        {
            if (req.HttpMethod == "HEAD") res.StatusCode = 200;
        });

Question 2: Is this single code implementation enough for all Routes defined in AppHost.

Routes.Add<dto1>("/GetMethod1", "GET");
Routes.Add<dto1>("/GetMethod1/Id/{ID}", "GET");
Routes.Add<dto2>("/GetMethod2", "GET");
Routes.Add<dto2>("/GetMethod2/Id/{ID}", "GET");

Thanks

1
What version of ServiceStack are you using? The new API design now lets you handle any HTTP Verb without having to use custom filters. - kampsj
ServiceStack - 3.9.4 ServiceStack.Common - 3.9.4 ServiceStack.Interface - 3.9.0 ServiceStack.OrmLite - 3.95 ServiceStack.ServiceInterface - 1.0 ServiceStack.Text - 3.9.5 - user836107
Guess this is the place I download the latest manually - github.com/ServiceStack/ServiceStack/downloads - user836107

1 Answers

4
votes

It looks like you're using an old version of ServiceStack, so I'd first recommend upgrading to the latest version.

Other than that the ideal way to handle HEAD or other uncommon HTTP Method requests is in using ServiceStack's new and more flexible API, e.g:

public void Head(GetMethod request) {
    base.Response.StatusCode = 200;
}

But just having the implementation existing is enough, so you could have an empty body. i.e. setting a 200 StatusCode is redundant since it's the default status code.

If you want to handle it generically using a filter, than you'll want to handle it as early in the Request Pipeline as possible. See ServiceStack's Order of Operations for the order in which user-defined events and filters get executed.