Gin: get request body

Created on 22 Mar 2018  Â·  9Comments  Â·  Source: gin-gonic/gin

i want get request body in function,but request body is empty.
code such as:
func LiveRecord(c *gin.Context) {
buf := make([]byte, 1024)
num, _ := c.Request.Body.Read(buf)
reqBody := string(buf[0:num])
}

but reqBody is empty,who can tell me how to use?

Most helpful comment

@liuzhiwang
That is correct, you are only able to read the body once in a handler that way. What are you trying to do that requires you to read it twice? If it is to bind the data twice, here is an issue that was resolved and merged into gin recently to address that scenario: #1341

Alternatively, you could add this line if you insist on making what you have work which will write the body back to the request after you read it:
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(reqBody)))

Example:

func test(c *gin.Context) {
    buf := make([]byte, 1024)
    num, _ := c.Request.Body.Read(buf)
    reqBody := string(buf[0:num])
    c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(reqBody))) // Write body back

    buf2 := make([]byte, 1024)
    num2, _ := c.Request.Body.Read(buf2)
    reqBody2 := string(buf2[0:num2])

    fmt.Println(reqBody, "==", reqBody2)
    c.JSON(http.StatusOK, reqBody)
}

All 9 comments

You should use c.GetRawData().

c.GetRawData() only works for requests with Content-Type as application/json. Check https://github.com/gin-gonic/gin/pull/1253. It could be a good feature 😊

Unless if I misunderstand the question, the code you provided worked for me when I tested it @lucky-lee ...

I added a line to return the reqBody in the response:

func test(c *gin.Context) {
    buf := make([]byte, 1024)
    num, _ := c.Request.Body.Read(buf)
    reqBody := string(buf[0:num])
    c.JSON(http.StatusOK, reqBody)
}

I started up the server:

[GIN-debug] GET / --> main.LiveRecord (4 handlers)
[GIN-debug] Listening and serving HTTP on :8080

Made a curl request to test it which worked:

jeff@host$ curl -X GET --data "Some data to return..." http://127.0.0.1:8080

"Some data to return..."

@jeffxf I found the problem because I called it once in the middleware, so I couldn't get it in the method.

@jeffxf
`func test(c *gin.Context) {
buf := make([]byte, 1024)
num, _ := c.Request.Body.Read(buf)
reqBody := string(buf[0:num])

buf2 := make([]byte, 1024)
num2, _ := c.Request.Body.Read(buf2)
reqBody2 := string(buf2[0:num2])

fmt.Println(reqBody, "==", reqBody2)
c.JSON(http.StatusOK, reqBody)

}`

you can run this demo, reqBody and reqBody2 have different value in fmt.Println

@liuzhiwang
That is correct, you are only able to read the body once in a handler that way. What are you trying to do that requires you to read it twice? If it is to bind the data twice, here is an issue that was resolved and merged into gin recently to address that scenario: #1341

Alternatively, you could add this line if you insist on making what you have work which will write the body back to the request after you read it:
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(reqBody)))

Example:

func test(c *gin.Context) {
    buf := make([]byte, 1024)
    num, _ := c.Request.Body.Read(buf)
    reqBody := string(buf[0:num])
    c.Request.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(reqBody))) // Write body back

    buf2 := make([]byte, 1024)
    num2, _ := c.Request.Body.Read(buf2)
    reqBody2 := string(buf2[0:num2])

    fmt.Println(reqBody, "==", reqBody2)
    c.JSON(http.StatusOK, reqBody)
}

@jeffxf right, please see #1341 , closing.

I'm here asking for the same issue:

  • i consume the request body first by .ShouldBindJSON
  • and then, i want to send the request body to other service.

I've tried to use GetRawBody, but that resulted in the same error. What i want is to pass-in the request body to different services/middlewares.

@jeffxf thanks!!!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iiinsomnia picture iiinsomnia  Â·  3Comments

olegsobchuk picture olegsobchuk  Â·  3Comments

cxk280 picture cxk280  Â·  3Comments

rawoke083 picture rawoke083  Â·  3Comments

gplume picture gplume  Â·  3Comments