Question

The context: I've built a REST service which handles 'Profile' objects. Each profile is required to have a unique name. One of the operations that clients will need to do for validation purposes is check to make sure that a profile with the given name does not already exist.

Rather than build a RPC-style 'ProfileExists' method, I would prefer to stay within REST design principles and issue a HEAD request to the Profile with the given name and then return the appropriate response code depending on whether the profile already exists or not (200, 404, respectively), no response body needed.

Following the conventions with the newer ServiceStack API, I've set up a method to accept Head requests and tested it successfully for both cases using Fiddler:

public object Head(GetProfile request)
{
    ValidateRequest(request);

    HttpStatusCode responseCode;

    using (var scope = new UnitOfWorkScope())
    {
        responseCode = _profileService.ProfileExists(request.Name) ? HttpStatusCode.OK : HttpStatusCode.NotFound;

        scope.Commit();
    }

    return new HttpResult { StatusCode = responseCode };
}

The trouble is on the client-side. Issuing the HEAD request through ServiceStack's IRestClient interface is proving difficult. While there are methods for Get, Post, Put, and Delete, there is no method for Head. From there I assumed I could use CustomMethod to specify the HEAD verb explicitly as a parameter:

public bool ProfileExists(string profileName)
{
    try
    {
        var response = _restClient.CustomMethod<IHttpResult>(HttpMethods.Head, new GetProfile { Name = profileName });

        return response.StatusCode == HttpStatusCode.OK;
    }
    catch (WebServiceException ex)
    {
        if (ex.StatusCode == 404)
            return false;
    }

    // Return false for any other reason right now.
    return false;
}

However, the underlying implementation (ServiceClientBase) throws an exception when validating the HttpVerb parameter:

if (HttpMethods.AllVerbs.Contains(httpVerb.ToUpper()))
                throw new NotSupportedException("Unknown HTTP Method is not supported: " + httpVerb);

The set HttpMethods.AllVerbs contains all of the usual verbs for RFC 2616 and more. Unless this behavior is a bug, throwing an exception for any of the known HTTP verbs suggests that the author's intent for CustomMethod did not include being able to issue requests for a known HTTP verb.

Which leads me to my question: How do I issue a HEAD request on the client side in ServiceStack?

Was it helpful?

Solution

This was a bug:

if (HttpMethods.AllVerbs.Contains(httpVerb.ToUpper()))
    throw new NotSupportedException("Unknown HTTP Method is not supported: " + httpVerb);

That I've just fixed in this commit. This fix will be available on the next release of ServiceStack (v3.9.33+) due this weekend.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top