feat: add GetRawData func. (#857)

* feat: add GetRawData func.

* update return style

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2017-03-31 08:45:56 +08:00 committed by GitHub
parent ad2dacedd6
commit 41316b9ca9
2 changed files with 17 additions and 0 deletions

View File

@ -7,6 +7,7 @@ package gin
import ( import (
"errors" "errors"
"io" "io"
"io/ioutil"
"math" "math"
"mime/multipart" "mime/multipart"
"net" "net"
@ -445,6 +446,11 @@ func (c *Context) GetHeader(key string) string {
return c.requestHeader(key) return c.requestHeader(key)
} }
// GetRawData return stream data
func (c *Context) GetRawData() ([]byte, error) {
return ioutil.ReadAll(c.Request.Body)
}
func (c *Context) SetCookie( func (c *Context) SetCookie(
name string, name string,
value string, value string,

View File

@ -1016,3 +1016,14 @@ func TestGetRequestHeaderValue(t *testing.T) {
assert.Equal(t, "1.0.0", c.GetHeader("Gin-Version")) assert.Equal(t, "1.0.0", c.GetHeader("Gin-Version"))
assert.Equal(t, "", c.GetHeader("Connection")) assert.Equal(t, "", c.GetHeader("Connection"))
} }
func TestContextGetRawData(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
body := bytes.NewBufferString("Fetch binary post data")
c.Request, _ = http.NewRequest("POST", "/", body)
c.Request.Header.Add("Content-Type", MIMEPOSTForm)
data, err := c.GetRawData()
assert.Nil(t, err)
assert.Equal(t, "Fetch binary post data", string(data))
}