1

Is there a way to check whether the resource at a given endpoint exists before sending out a GET request?

  • See [this question](https://stackoverflow.com/questions/16539269/http-head-vs-get-performance) – Bart Platak Jan 04 '16 at 20:57
  • 2
    You can send a Head request? There's no way for you to know unless you make contact with the server – Eric Guan Jan 04 '16 at 20:57
  • How would I 'send a Head request'? Will that return a boolean? –  Jan 04 '16 at 20:59
  • 1
    http://stackoverflow.com/questions/333634/http-head-request-in-javascript-ajax you send the request then check the response status – Paolo Jan 04 '16 at 21:05

3 Answers3

8

HEAD, but whether or not it's actually worth it depends on the resource you're requesting, really.

Or create a new request that checks for resource existence and use the results of that to determine if you should bother fetching the resource.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • That's what I'm trying to get at: to determine whether I am going to fetch it in the future or not. –  Jan 04 '16 at 21:03
  • 1
    @NikitaTomkevich *Going* to, or *can*? In any case, if it's a small resource, I'd just grab it. If it isn't, or you're optimizing for mobile or something, then a HEAD request is probably what you want. As to what it returns, it depends on what you're calling. For example, IIRC Rails used to handle a HEAD request as a GET so you had to check manually and use a `head` response. – Dave Newton Jan 04 '16 at 21:56
  • I'm not sure why this wasn't immediately obvious to me, but after I banged my head against my keyboard for a while, pun intended, I finally figured out that HEAD was just another REST method like GET or POST. This insight might help somebody. – OCDev Apr 30 '22 at 17:27
0

The only way to check if a resource is available is to try to fetch it and see if it exists.

You could cache a list and at least you wouldnt need to try twice but there is no way around it the first time.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
0

JavaScript supports Try/Catch statements, we can give more detailed answers if specifically referenced functions or libraries are mentioned in the question.

see also:

Community
  • 1
  • 1
Jason K.
  • 407
  • 4
  • 12