Created
July 25, 2019 22:34
-
-
Save gopye/1efaf33047e137112b5ddfcefb501d03 to your computer and use it in GitHub Desktop.
Go "GET" request with content-type headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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