我有一個 http 請求,我需要檢查其正文。但是當(dāng)我這樣做時,請求失敗了。我假設(shè)這與 Reader 需要重置有關(guān),但是谷歌搜索并go ioutil reset ReadCloser沒有發(fā)現(xiàn)任何看起來有希望的東西。c是*middleware.Context, c.Req.Request是http.Request, c.Req.Request.Body是io.ReadClosercontents, _ := ioutil.ReadAll(c.Req.Request.Body)log.Info("Request: %s", string(contents))proxy.ServeHTTP(c.RW(), c.Req.Request)具體來說,我得到的錯誤是 http: proxy error: http: ContentLength=133 with Body length 0
1 回答

手掌心
TA貢獻1942條經(jīng)驗 獲得超3個贊
您無法重置它,因為您已經(jīng)從中讀取了內(nèi)容并且流中沒有任何內(nèi)容。
你可以做的是獲取你已經(jīng)擁有的緩沖字節(jié),并用一個新的替換 Body io.ReadCloser
contents, _ := ioutil.ReadAll(c.Req.Request.Body)
log.Info("Request: %s", string(contents))
c.Req.Request.Body = ioutil.NopCloser(bytes.NewReader(contents))
proxy.ServeHTTP(c.RW(), c.Req.Request)
- 1 回答
- 0 關(guān)注
- 190 瀏覽
添加回答
舉報
0/150
提交
取消