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 關注
- 176 瀏覽
添加回答
舉報