Golang : Check a web page existence with HEAD request example
There are times when we need to check if a web server is up and running first before querying further or simply want to check if a web page exists or not without downloading the HTML content. In this tutorial, we will learn how to utilize the HEAD
request to ask for a response status from the web server without the response body. This also can be useful in a situation where we want to retrieve meta-information in the response headers, but without the response body.
The code example below will query a given URL from command line parameter and if the web page exists, it will return valid HTTP response codes such as OK, FOUND or MOVED PERMANENTLY. If the web page is not online, the program will panic and exit.
Pay attention to how some web servers handle HTTP
differently from HTTPS
. Depending on what you after, sometimes you need to query both HTTP
and HTTPS
version of a web page to determine their actual availability.
NOTE: If you get this error message unsupported protocol scheme ""
, make sure to put http
or https
before the domain name.
Here you go!
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <URL> \n", os.Args[0])
os.Exit(0)
}
url := os.Args[1]
fmt.Println("Heading ", url)
client := &http.Client{}
resp, err := client.Head(url)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
fmt.Println(url, " status is : ", resp.Status)
}
Sample output:
$ ./headrequest https://www.facebook.com
Heading https://www.facebook.com
https://www.facebook.com status is : 200 OK
$ ./headrequest https://www.socketloop.com
Heading https://www.socketloop.com
https://www.socketloop.com status is : 200 OK
$ ./headrequest https://www.porkchop.com
Heading https://www.porkchop.com
Head https://www.porkchop.com: read tcp 192.168.1.69:33176->69.172.201.153:443: read: connection reset by peer
$ ./headrequest http://www.porkchop.com
Heading http://www.porkchop.com
http://www.porkchop.com status is : 200 OK
$ ./headrequest https://www.punyu.com
Heading https://www.punyu.com
Head https://www.punyu.com: x509: certificate is valid for *.asg.to, asg.to, not www.punyu.com
$ ./headrequest http://www.punyu.com
Heading http://www.punyu.com
http://www.punyu.com status is : 200 OK
Reference:
https://www.socketloop.com/references/golang-net-http-client-postform-function-example
See also : Golang : Command line file upload program to server example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7k Golang : Scanf function weird error in Windows
+11.1k Swift : Convert (cast) Float to String
+9.2k Javascript : Read/parse JSON data from HTTP response
+19.3k Golang : Count JSON objects and convert to slice/array
+9.9k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+15.5k Golang : Generate universally unique identifier(UUID) example
+16k Golang :Trim white spaces from a string
+5.2k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+8k Golang : Count leading or ending zeros(any item of interest) example
+10.8k Android Studio : Create custom icons for your application example
+7.8k Golang : Qt splash screen with delay example
+6.3k Golang : Find the longest line of text example