當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。