user3373877
user3373877

Reputation: 325

How to check if a web file exists or not by using golang?

I want to check if a large file exists on a web server using golang :

resp, err := http.Get("http://aa.com/aa.mp4")
if err != nil {
   return false
  }
if resp.StatusCode != http.StatusOK {
    return false
}

I can get what i want, but the aa.mp4 is a large file, so this way looks nonoptimal. Is there another way?

Upvotes: 3

Views: 2216

Answers (1)

icchanobot
icchanobot

Reputation: 3343

You can do a HEAD request instead http.Head()

It is the same as a GET but won't download the body.

resp, err := http.Head("http://aa.com/aa.mp4")
  if err != nil {
   return false
  }
  if resp.StatusCode != http.StatusOK {
    return false
  }

From the HTTP spec:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response

Upvotes: 9

Related Questions