mirror of https://github.com/gin-gonic/gin.git
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:
parent
ad2dacedd6
commit
41316b9ca9
|
@ -7,6 +7,7 @@ package gin
|
|||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
|
@ -445,6 +446,11 @@ func (c *Context) GetHeader(key string) string {
|
|||
return c.requestHeader(key)
|
||||
}
|
||||
|
||||
// GetRawData return stream data
|
||||
func (c *Context) GetRawData() ([]byte, error) {
|
||||
return ioutil.ReadAll(c.Request.Body)
|
||||
}
|
||||
|
||||
func (c *Context) SetCookie(
|
||||
name string,
|
||||
value string,
|
||||
|
|
|
@ -1016,3 +1016,14 @@ func TestGetRequestHeaderValue(t *testing.T) {
|
|||
assert.Equal(t, "1.0.0", c.GetHeader("Gin-Version"))
|
||||
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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue