GoLang: A Command-Line HTTP Client Tool to Grab URL Contents


GoLang provides HTTP client library. We can import the “net/http” to make a HTTP Get. Then we can use the bufio.NewScanner to print the content Line by Line or we can directly print out the content using fmt.Print(response.Body).

We can take a paramter of URL string at command line using the os.Args (which contains the program path).

If there is any error, we can throw errors using the panic function. And we can set return/exit code using os.Exit() function.

We can convert the status string into Integer using strconv.Atoi function. We can use the strings.Fields or strings.Split(str, delimier) to split a string by whitespaces.

Overall, the GoLang command line HTTP tool source code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
 
import (
    "bufio"
    "fmt"
    "net/http"
    "os"
)
 
func main() {
    if len(os.Args) == 1 {
        panic("Need a URL parameter")
        os.Exit(1)
    }
    var url = os.Args[1]
 
    resp, err := http.Get(url)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
 
    scanner := bufio.NewScanner(resp.Body)
    for i := 0; scanner.Scan(); i++ {
        fmt.Println(scanner.Text())
    }
    if err := scanner.Err(); err != nil {
        panic(err)
    }
 
    //var v = strings.Fields(resp.Status)
    //intVar, err := strconv.Atoi(v[0])
 
    fmt.Println("Status = " + resp.Status)
    os.Exit(0)
}
package main

import (
	"bufio"
	"fmt"
	"net/http"
	"os"
)

func main() {
	if len(os.Args) == 1 {
		panic("Need a URL parameter")
		os.Exit(1)
	}
	var url = os.Args[1]

	resp, err := http.Get(url)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	scanner := bufio.NewScanner(resp.Body)
	for i := 0; scanner.Scan(); i++ {
		fmt.Println(scanner.Text())
	}
	if err := scanner.Err(); err != nil {
		panic(err)
	}

	//var v = strings.Fields(resp.Status)
	//intVar, err := strconv.Atoi(v[0])

	fmt.Println("Status = " + resp.Status)
	os.Exit(0)
}

Example usage:

1
2
3
$ go run url.go https://helloacm.com/api/phpver
"7.3.14-6+ubuntu18.04.1+deb.sury.org+1"
Status = 200 OK
$ go run url.go https://helloacm.com/api/phpver
"7.3.14-6+ubuntu18.04.1+deb.sury.org+1"
Status = 200 OK

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
315 words
Last Post: Teaching Kids Programming - Depth First Search Algorithm to Count the Number of Islands
Next Post: Teaching Kids Programming - Binary Tree Inorder Traversal via Recursion or Iteration

The Permanent URL is: GoLang: A Command-Line HTTP Client Tool to Grab URL Contents

Leave a Reply