Bartosz
Bartosz

Reputation: 4602

Passing boolean value through HttpResponse

What is the most appropriate (and elegant?) way to pass boolean value of true or false through HttpResponse. I need it for a GetCustomerExist method (using ASP.NET Web API). Am writing it to the body of the message, but feel it's not the wright way of doing it. Can I pass it in the response header somehow?

Upvotes: 1

Views: 4280

Answers (1)

Markus
Markus

Reputation: 22481

Instead of creating a method that checks for the existence of a customer, a more "RESTful" way would be to send a HEAD request to the customer resource. If the customer doesn't exist, answer with 404 Not Found else with 200 OK. In a HEAD request, you don't have to provide the message body.

In a quick sample, I was able to implement this in WebAPI like this. In the sample, odd ids do exist, even ones return a 404. Also, I add a fantasy header value to the response:

public HttpResponseMessage Head(int id)
{
    HttpResponseMessage msg;
    if (id % 2 == 0)
        msg = new HttpResponseMessage(HttpStatusCode.NotFound);
    else
        msg = new HttpResponseMessage(HttpStatusCode.OK);
    msg.Headers.Add("Hello", "World");
    return msg;
}

Upvotes: 2

Related Questions