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)
}
}
@Erichain
Copy link

Thanks for sharing, it's so great.

@Kundeshi
Copy link

Kundeshi commented Jan 9, 2017

This info was very helpful, however. Is there a way to pick out just the response status?

@bengadbois
Copy link

The response status (200, 404, etc.) is available in response.StatusCode. You don't need to read the body for the status code.

@Naveennani777
Copy link

Naveennani777 commented Jul 21, 2017

Hi All,
I am not able to see content of get response from a server . Could you please let me know how to get the response .Am seeing hexadecimal values from output

Pasted my code below:
+++++++++++++++
package main
import (
"fmt"
"net/http"
"io"
"os"
// "encoding/json"

)

func main() {
url := "http://localhost:8080/api/v1/networks"
res, err := http.Get(url)
if err != nil {
panic(err)
}
fmt.Println("response from GET request" ,res)
fmt.Println("output of pnmid", res.Body)
defer res.Body.Close()
, err := io.Copy(os.Stdout, response.Body)
// fmt.Println("whole body",
)

body, err := ioutil.ReadAll(res.Body)
if err != nil {
	panic(err)
}
fmt.Println("response body" , res.Body)

Please help me on this to fix the issue.

@Naveennani777
Copy link

I tried in below way and got output:

package main
import (
"fmt"
"net/http"
// "io"
// "os"
// "encoding/json"

)

func main() {
url := "http://localhost:3000/api/v1/subnets"
res, err := http.Get(url)
if err != nil {
panic(err)
}
fmt.Println("response from GET request" ,res)
fmt.Println("output of pnmid", res.Body)
defer res.Body.Close()
// , err := io.Copy(os.Stdout, response.Body)
// fmt.Println("whole body",
)

/* body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}*/
fmt.Println("response body" , res.Body)
// fmt.Println(body)
// var p Payload

// err := json.Unmarshal(body , &p)

}

After execution:

go run Sample_restcall.go:

response from GET request &{200 OK 200 HTTP/1.1 1 1 map[Access-Control-Allow-Origin:[*] Content-Type:[application/json; charset=utf-8] Set-Cookie:[dcbfef97e14c2a3cdf00a5
afc32d1e96=d9871f0ca4d0f07b0c6b84a4d79d8153; path=/; HttpOnly] X-Powered-By:[Express] Access-Control-Allow-Headers:[Content-Type] Access-Control-Allow-Methods:[PUT, GET,
POST, DELETE, OPTIONS] Content-Length:[676] Etag:[W/"2a4-JHwpbMIyxGxUzMU+0J4onQ"] Date:[Fri, 21 Jul 2017 15:24:47 GMT] Cache-Control:[private]] 0xc04203e300 676 [] fals
e false map[] 0xc042030400 }
output of pnmid &{0xc04203e2c0 {0 0} false 0x5cb870 0x5cb800}
response body &{0xc04203e2c0 {0 0} false 0x5cb870 0x5cb800}

I could see response status code as 200 , how to see response content and how to decode to normal json format.

@sibyakin
Copy link

Naveennani777, you see what it is, slice of bytes, you have to convert it to string, try this:

        body, _ := ioutil.ReadAll(res.Body)
        text := string(body)
        fmt.Println(text)

@Naveennani777
Copy link

Thanks alot sibyakin , Its working now.

@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