Farray
Farray

Reputation: 8548

How do I use an arbitrary HTTP verb in a ServiceStack REST service?

When using ServiceStack to create a REST service, how do you handle arbitrary HTTP verbs?

The most common verbs have very simple methods. For example, to handle a GET request I would do the following:

public class MyService : RestServiceBase<MyRequest>
{
    public override object OnGet(MyRequest request){
        //do stuff and return HttpResult or object for serialization
    }
}

Similar overrides exist for PUT, POST, DELETE, and PATCH.

What do I do if I want to use HEAD or some other verb?

(I've looked through the documentation and examples and other ServiceStack questions but haven't found anything that indicates the correct way to do this.)

Upvotes: 1

Views: 580

Answers (1)

mythz
mythz

Reputation: 143399

ServiceStack's RestServiceBase class already has built-in support for GET, POST, PUSH, DELETE and PATCH. so you just override it as you would your OnGet() method, e.g. you can handle DELETE requests with:

public override object OnDelete(MyRequest request){
    //do stuff and return HttpResult or object for serialization
}

For HEAD requests you currently can't handle this in a ServiceStack service so you will need to handle it in either a RequestFilter or RequestFitler Attribute (make sure you close the httpRes to terminate the request).

You also have the opportunity to by-pass the ServiceStack pipeline completely by registering your own Custom IHttpHandler in the EndpointHostConfig.RawHttpHandlers config in your AppHost.

Upvotes: 1

Related Questions