mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
reuse http.request.Body
This commit is contained in:
parent
5e40c1d49c
commit
5460b28fa4
11
context.go
11
context.go
@ -5,6 +5,7 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -78,6 +79,9 @@ type Context struct {
|
||||
// SameSite allows a server to define a cookie attribute making it impossible for
|
||||
// the browser to send this cookie along with cross-site requests.
|
||||
sameSite http.SameSite
|
||||
|
||||
// rawData cache and reuse http.request.Body
|
||||
rawData []byte
|
||||
}
|
||||
|
||||
/************************************/
|
||||
@ -485,6 +489,10 @@ func (c *Context) initFormCache() {
|
||||
if c.formCache == nil {
|
||||
c.formCache = make(url.Values)
|
||||
req := c.Request
|
||||
if req.Body != nil {
|
||||
c.rawData, _ = ioutil.ReadAll(req.Body)
|
||||
req.Body = ioutil.NopCloser(bytes.NewReader(c.rawData))
|
||||
}
|
||||
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
|
||||
if err != http.ErrNotMultipart {
|
||||
debugPrint("error on parse multipart form array: %v", err)
|
||||
@ -792,7 +800,8 @@ func (c *Context) GetHeader(key string) string {
|
||||
|
||||
// GetRawData return stream data.
|
||||
func (c *Context) GetRawData() ([]byte, error) {
|
||||
return ioutil.ReadAll(c.Request.Body)
|
||||
// return ioutil.ReadAll(c.Request.Body)
|
||||
return c.rawData, nil
|
||||
}
|
||||
|
||||
// SetSameSite with cookie
|
||||
|
Loading…
x
Reference in New Issue
Block a user