I'm working to create a little console for manage digitalocean droplets, this is my first experience with Go..
I have this error
cannot use s (type []byte) as type io.Reader in argument to http.NewRequest:
[]byte does not implement io.Reader (missing Read method)
How can i convert s []bytes in a good type of value for func NewRequest?! NewRequest expect Body of type io.Reader..
s, _ := json.Marshal(r);
// convert type
req, _ := http.NewRequest("GET", "https://api.digitalocean.com/v2/droplets", s)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("Content-Type", "application/json")
response, _ := client.Do(req)
Thanks!
As @elithrar says use bytes.NewBuffer
b := bytes.NewBuffer(s)
http.NewRequest(..., b)
That will create a *bytes.Buffer
from []bytes
. and bytes.Buffer
implements the io.Reader interface that http.NewRequest
requires.