go snippet for go

created : 2022-09-02T13:06:05+00:00
modified : 2022-09-02T13:30:04+00:00

go http snippet

Get, Post Snippet

url := "<url>"

resp, err := http.Get(url)
// resp, err := http.Post(url, "text/plain", reqBody) // reqBody []bytes
// resp, err := http.PostForm(url, url.Values{"Foo":{"Bar"}})
// resp, err := http.Post(url, "application/json", reqBody) // reqBody []bytes; reqBody, err := json.Marshal(data)
if err != nil {
  return err
}
defer resp.Body.Close()

raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
  return err
}

data := struct{
  A, B int
}{}
err = json.Unmarshal(raw, &data)
if err != nil {
  return err
}

With Auth

url := "<url>"
bearer := "Bearer <token>"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
  return err
}

req.Header.Add("Authorization", bearer)
resp, err := http.Do(req)
// skip