1

My WEB API has a single GET method as mentioned below which will return a resource based on the id.

public HttpResponseMessage Get(string id)
{
   ....      
}

I also have a requirement wherein I need to check whether a resource exists or not(no need to return resource).

I would like to know , whether as per the true RESTful service do I need to write a separate GET for second requirement or use the existing one.

6
  • You could always return a NotFound(). Commented Oct 14, 2015 at 9:09
  • 1
    I would always expect a GET for a single resource (with an id) to return a 404 if it does not exist. Commented Oct 14, 2015 at 9:12
  • @ Mike Eason , so are you suggesting for a new method Commented Oct 14, 2015 at 9:14
  • @CleanCrispCode I don't believe he is suggesting that. Simply do your query as normal in the above Get if you can't find what they want, return NotFound() (so 404) or if you do, return it as normal (200). The user of the API will know if it exists or not from a single call. Commented Oct 14, 2015 at 9:23
  • When I only want to check whether resource exists or not , why need to return the resource , thats my only concern. Commented Oct 14, 2015 at 9:37

2 Answers 2

1

We can use IHttpActionResult of Web API 2 for these kinds of requirements. Here is code snippet. Using IHttpActionResult will give proper response to clients with HTTP status codes and response object.

public IHttpActionResult Get(string id)
        {
            //IsResourceExists is method taking  id and return response
            var response = IsResourceExists(id);

            //Assuming 'response' is TRUE
            if (response)
            {
                return Ok(response);
            }
            else
            {
                return NotFound();
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

0

As described in your comment, you wish to be able to check if the resource exists without returning it.

For this, you can use HEAD as defined here.

9.4 HEAD

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

I don't have code to provide you with as an answer on how to implement HEAD requests however a quick search yielded this result also google cached if direct is down.

Hope that points you in the correct direction!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.