2 回答
TA贡献1793条经验 获得超6个赞
Request有 aContentLength作为可用属性,在某些情况下您可能只能使用它。虽然如果请求使用传输编码,我认为这个值设置为 -1(可能为 0)。
否则我认为你可以用你自己的 io.ReadCloser 实现包装 req.Body 。像这样:
type RecordLengthReadCloser struct {
io.ReadCloser
length int
}
func (rc *RecordLengthReadCloser) Read(p []byte) (int, error) {
n, err := rc.ReadCloser.Read(p)
rc.length += n
return n, err
}
func handleHTTP(w http.ResponseWriter, req *http.Request) {
rc := &RecordLengthReadCloser{ReadCloser: req.Body}
req.Body = rc
resp, err := http.DefaultTransport.RoundTrip(req)
fmt.Println(rc.length)
_, _ = resp, err
}
这可能有我不知道的问题,我不确定你是否可以自由地重新分配 req.Body 没有问题。
TA贡献1895条经验 获得超7个赞
http.Request.Body是类型io.ReadCloser。它是一个interface,您可以利用它来用您自己的实现来包装 Body 值,io.ReadCloser该实现计算从中读取的字节数。
package main_test
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestRequestLen(t *testing.T) {
reqBody := "Hello world!"
resBody := "Hello, client"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, resBody)
}))
defer ts.Close()
req, err := http.NewRequest(http.MethodGet, ts.URL, strings.NewReader(reqBody))
if err != nil {
log.Fatal(err)
}
req.Body = &bytesRead{ReadCloser: req.Body}
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
if want := resBody; string(greeting) != want {
t.Fatalf("invalid response body %q want %q", string(greeting), want)
}
if want := len(reqBody); req.Body.(*bytesRead).total != want {
t.Fatalf("invalid request length %v want %v", req.Body.(*bytesRead).total, want)
}
}
type bytesRead struct {
io.ReadCloser
total int
}
func (c *bytesRead) Read(p []byte) (n int, err error) {
n, err = c.ReadCloser.Read(p)
c.total += n
return
}
- 2 回答
- 0 关注
- 124 浏览
添加回答
举报