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?
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:
ShouldBindJSONI'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!!!!!
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: