Re: [go-nuts] How to read a JSON Response body

2018-03-20 Thread st ov
Thanks! So using ioutil.ReadAll() followed by json.Unmarshal() can still be used, as this official example in the documentation shows https://golang.org/pkg/net/http/#example_Get But is less efficient than using json.Decoder().Decode() itself How should I handle the EOF case? On Tuesday

Re: [go-nuts] How to read a JSON Response body

2018-03-20 Thread Jonathan Yu
Something to consider is that NewDecoder supports JSON streams, e.g. {"a": "b"}{"c": "d"}{"e": "f" } Each call to Decode will decode one object from the stream, but the underlying reader is not necessarily guaranteed to be at EOF. See: https://ahmet.im/blog/golang-json-decoder-pitfalls/ I don't h

Re: [go-nuts] How to read a JSON Response body

2018-03-20 Thread Alex Efros
Hi! On Tue, Mar 20, 2018 at 10:37:40AM -0700, st ov wrote: > json.Unmarshal(resp.Body, &data) This one is invalid. > json.NewDecoder(resp.Body).Decode(&data) > > or > > b, _ := ioutil.ReadAll(resp.Body) > json.Unmarshal(b, &data) In the ReadAll case you'll have to allocate []byte in memory to