【发布时间】:2014-09-30 02:01:51
【问题描述】:
我正在创建一个用于管理 DigitalOcean Droplets 的小控制台,但出现此错误:
不能在 http.NewRequest 的参数中使用 s(类型 []byte)作为类型 io.Reader: []byte 没有实现 io.Reader(缺少 Read 方法)
如何将 s []bytes 转换为 func NewRequest 的良好值类型?! NewRequest 期望 Body 类型为 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)
【问题讨论】:
-
第一:检查错误并处理它们。第二:golang.org/pkg/bytes/#NewBuffer
-
感谢作品!很容易
标签: json go type-conversion