Is there any library/package that returns the json string received in response to an http request. It's pretty straightforward, so I can write my own, but would prefer existing/tested code over reinventing the wheel.
Currently, I have:
func getJsonStr(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{0}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{0}, err
}
return body, nil
}
EDIT: I'm looking for something like Node's 'request' module, which lets me do it in one line, like so: jsonStr, err := getJsonStr(url).
return nil, err
instead of what you have which is allocating a one element slice no one should be looking at anyway (if you really want an non-nil but empty slice it would be[]byte{}
). If you want a one liner, then just write the above function somewhere once and use the exact one line you give wherever you need it.