u, _ := url.Parse(endpoint)
q := u.Query()
q.Add("to", lang)
u.RawQuery = q.Encode()
body := []struct {
Text string
}{
{Text: text},
}
b, _ := json.Marshal(body)
req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(b))
if err != nil {
log.Fatal(err)
}
req.Header.Add("Key", apikey)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
var result interface{}
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
log.Fatal(err)
}
final, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("%s\n", final)
Why is making http requests in Go so elaborate? There should be an easier way to translate the received info into JSON without using interfaces and sending requests without making structs.