GO語言"net/http/httputil"包中"DumpRequest"函數的用法及代碼示例。
用法:
func DumpRequest(req *http.Request, body bool)([]byte, error)
DumpRequest 在其 HTTP/1.x 線路表示中返回給定的請求。它隻能由服務器用於調試客戶端請求。返回的表示隻是一個近似值;初始請求的一些細節在解析為 http.Request 時會丟失。特別是,標頭字段名稱的順序和大小寫丟失。 multi-valued 標頭中的值順序保持不變。 HTTP/2 請求以 HTTP/1.x 形式轉儲,而不是其原始二進製表示形式。
如果 body 為真,DumpRequest 也會返回正文。為此,它消耗 req.Body,然後用產生相同字節的新 io ReadCloser 替換它。如果DumpRequest 返回錯誤,則 req 的狀態未定義。
http.Request.Write 的文檔詳細說明了轉儲中包含哪些 req 字段。
例子:
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"strings"
)
func main() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%q", dump)
}))
defer ts.Close()
const body = "Go is a general-purpose language designed with systems programming in mind."
req, err := http.NewRequest("POST", ts.URL, strings.NewReader(body))
if err != nil {
log.Fatal(err)
}
req.Host = "www.example.org"
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", b)
}
輸出:
"POST / HTTP/1.1\r\nHost: www.example.org\r\nAccept-Encoding: gzip\r\nContent-Length: 75\r\nUser-Agent: Go-http-client/1.1\r\n\r\nGo is a general-purpose language designed with systems programming in mind."
相關用法
- GO DumpRequestOut用法及代碼示例
- GO DumpResponse用法及代碼示例
- GO Dumper用法及代碼示例
- GO Dump用法及代碼示例
- GO Duration.Hours用法及代碼示例
- GO Duration.Round用法及代碼示例
- GO Duration用法及代碼示例
- GO Duration.Truncate用法及代碼示例
- GO Duration.String用法及代碼示例
- GO Duration.Minutes用法及代碼示例
- GO Duration.Milliseconds用法及代碼示例
- GO Duration.Seconds用法及代碼示例
- GO Duration.Microseconds用法及代碼示例
- GO Duration.Nanoseconds用法及代碼示例
- GO DecodeLastRuneInString用法及代碼示例
- GO DB.QueryRowContext用法及代碼示例
- GO Date用法及代碼示例
- GO DB.ExecContext用法及代碼示例
- GO Dial用法及代碼示例
- GO DB.BeginTx用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 DumpRequest。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。