diff --git a/beego.go b/beego.go index f32d948d..ca411af7 100644 --- a/beego.go +++ b/beego.go @@ -45,6 +45,7 @@ var ( HttpServerTimeOut int64 ErrorsShow bool XSRFKEY string + CopyRequestBody bool ) func init() { diff --git a/config.go b/config.go index f652a38b..af90bb87 100644 --- a/config.go +++ b/config.go @@ -186,6 +186,9 @@ func ParseConfig() (err error) { if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil { ErrorsShow = errorsshow } + if copyrequestbody, err := AppConfig.Bool("copyrequestbody"); err == nil { + CopyRequestBody = copyrequestbody + } if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" { XSRFKEY = xsrfkey } diff --git a/context.go b/context.go index 01428c34..6331dc0a 100644 --- a/context.go +++ b/context.go @@ -11,6 +11,7 @@ import ( type Context struct { ResponseWriter http.ResponseWriter Request *http.Request + RequestBody []byte Params map[string]string } diff --git a/router.go b/router.go index 37f45899..4029c8e7 100644 --- a/router.go +++ b/router.go @@ -1,7 +1,9 @@ package beego import ( + "bytes" "fmt" + "io/ioutil" "net/http" "net/url" "os" @@ -258,6 +260,18 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) requestPath := r.URL.Path + var requestbody []byte + + if CopyRequestBody { + requestbody, _ = ioutil.ReadAll(r.Body) + + r.Body.Close() + + bf := bytes.NewBuffer(requestbody) + + r.Body = ioutil.NopCloser(bf) + } + r.ParseMultipartForm(MaxMemory) //user defined Handler @@ -346,7 +360,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) params[route.params[i]] = match } //reassemble query params and add to RawQuery - r.URL.RawQuery = url.Values(values).Encode() + "&" + r.URL.RawQuery + r.URL.RawQuery = url.Values(values).Encode() //r.URL.RawQuery = url.Values(values).Encode() } runrouter = route @@ -370,7 +384,7 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) //call the controller init function init := vc.MethodByName("Init") in := make([]reflect.Value, 2) - ct := &Context{ResponseWriter: w, Request: r, Params: params} + ct := &Context{ResponseWriter: w, Request: r, Params: params, RequestBody: requestbody} in[0] = reflect.ValueOf(ct) in[1] = reflect.ValueOf(runrouter.controllerType.Name())