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


GO DumpRequestOut用法及代码示例


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

用法:

func DumpRequestOut(req *http.Request, body bool)([]byte, error)

DumpRequestOut 类似于 DumpRequest 但用于传出客户端请求。它包括标准 http.Transport 添加的任何标头,例如User-Agent。

例子:

package main

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

func main() {
	const body = "Go is a general-purpose language designed with systems programming in mind."
	req, err := http.NewRequest("PUT", "http://www.example.org", strings.NewReader(body))
	if err != nil {
		log.Fatal(err)
	}

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

	fmt.Printf("%q", dump)

}

输出:

"PUT / HTTP/1.1\r\nHost: www.example.org\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 75\r\nAccept-Encoding: gzip\r\n\r\nGo is a general-purpose language designed with systems programming in mind."

相关用法


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