Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get request body #1295

Closed
lucky-lee opened this issue Mar 22, 2018 · 9 comments
Closed

get request body #1295

lucky-lee opened this issue Mar 22, 2018 · 9 comments

Comments

@lucky-lee
Copy link

lucky-lee commented Mar 22, 2018

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?

@thinkerou
Copy link
Member

You should use c.GetRawData().

@davidbayo10
Copy link

c.GetRawData() only works for requests with Content-Type as application/json. Check #1253. It could be a good feature 😊

@jeffxf
Copy link

jeffxf commented Apr 11, 2018

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..."

@lucky-lee
Copy link
Author

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

@741369
Copy link

741369 commented Jul 2, 2018

@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

@jeffxf
Copy link

jeffxf commented Jul 3, 2018

@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)
}

@thinkerou
Copy link
Member

@jeffxf right, please see #1341 , closing.

@adonese
Copy link

adonese commented Nov 2, 2018

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.

@iineva
Copy link

iineva commented Mar 8, 2019

@jeffxf thanks!!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants