当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


GO DumpResponse用法及代码示例


GO语言"net/http/httputil"包中"DumpResponse"函数的用法及代码示例。

用法:

func DumpResponse(resp *http.Response, body bool)([]byte, error)

DumpResponse 类似于 DumpRequest 但会转储响应。

例子:

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"
)

func main() {
	const body = "Go is a general-purpose language designed with systems programming in mind."
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Date", "Wed, 19 Jul 1972 19:00:00 GMT")
		fmt.Fprintln(w, body)
	}))
	defer ts.Close()

	resp, err := http.Get(ts.URL)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	dump, err := httputil.DumpResponse(resp, true)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%q", dump)

}

输出:

"HTTP/1.1 200 OK\r\nContent-Length: 76\r\nContent-Type: text/plain; charset=utf-8\r\nDate: Wed, 19 Jul 1972 19:00:00 GMT\r\n\r\nGo is a general-purpose language designed with systems programming in mind.\n"

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 DumpResponse。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。