0

I would like to introduce a HEAD Web API endpoint that will send 200 if a resource exists or 404 if it doesn't.

HEAD api/resource/{id}

How this can be handled in AngularJS $http.head() without the need of boiler plate code to check the status so it returns true or false? Or is it that 200 will be considered success and 404 failure? Any help is appreciated!

Thanks in advance!

Michail

4
  • What do you mean by boiler plate code? Commented Oct 14, 2015 at 14:26
  • The same code over and over to check the status that head returned.. Except if 404 is considered failure and 200 a success Commented Oct 14, 2015 at 14:27
  • This is what services are for. Commented Oct 14, 2015 at 14:29
  • simple service is all you need Commented Oct 14, 2015 at 14:37

1 Answer 1

3

I actually found it.. From the documentation

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.

$http.head('/api/resource/'+someId, config).then(
   function() { return true;}, 
   function() { return false;}
);

$http documentation

Sign up to request clarification or add additional context in comments.

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.