Skip to content

Instantly share code, notes, and snippets.

@gopye
Created July 25, 2019 22:34
Show Gist options
  • Save gopye/1efaf33047e137112b5ddfcefb501d03 to your computer and use it in GitHub Desktop.
Save gopye/1efaf33047e137112b5ddfcefb501d03 to your computer and use it in GitHub Desktop.
Go "GET" request with content-type headers
package main
import (
"fmt"
"log"
"net/http"
"io/ioutil"
)
func Request(url string) []byte {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return body
}
func main() {
url := "https://api.coingecko.com/api/v3/ping"
saveStr := Request(url)
s := string(saveStr)
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment