1 回答

TA贡献1796条经验 获得超7个赞
一个网址。请求对象具有 Write 方法:
func (r *Request) Write(w io.Writer) error
写入以有线格式写入 HTTP/1.1 请求,即标头和正文。
您可以使用它将字节写入缓冲区对象。例如:
package main
import (
"bytes"
"fmt"
"net/http"
)
func main() {
var buf bytes.Buffer
req, err := http.NewRequest("GET", "http://google.com", nil)
if err != nil {
panic(err)
}
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
panic(err)
}
defer res.Body.Close()
if err := res.Write(&buf); err != nil {
panic(err)
}
// ...do whatever you want with the buffer here...
fmt.Println(buf.String())
}
Buffer 对象具有 Bytes 方法,该方法将返回一个字节数组(如果需要)。
- 1 回答
- 0 关注
- 184 浏览
添加回答
举报