Skip to content

Instantly share code, notes, and snippets.

@ijt
Last active August 21, 2024 05:11
Show Gist options
  • Save ijt/950790 to your computer and use it in GitHub Desktop.
Save ijt/950790 to your computer and use it in GitHub Desktop.
Example of using http.Get in go (golang)
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s URL\n", os.Args[0])
os.Exit(1)
}
response, err := http.Get(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
_, err := io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal(err)
}
}
@nurtureJamesTan
Copy link

thank you :)

@pprasanthi
Copy link

Thank you :)

@chandankumar4
Copy link

How to pass header in GET request

@otaviorojas
Copy link

How to pass header in GET request

@cbess
Copy link

cbess commented Oct 10, 2019

How to pass header in GET request

Here is an example:

client := &http.Client{}
apiURL := "https://myapi.url/data"
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil {
	return nil, err
}

// add header to request
req.Header.Add("authkey", "...")

// perform get request
res, err := client.Do(req)

@R4wm
Copy link

R4wm commented Oct 25, 2019

You dont need the else since your os.Exit'ing on condition of Line 12

@ijt
Copy link
Author

ijt commented Oct 28, 2019

@R4wm, done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment