Description

Why is the length of read context.request.body 0 after calling the context.postform() function ?

How to reproduce

package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    g := gin.Default()
    g.GET("/hello/:name", func(c *gin.Context) {


          //  If the postform function is called here, next function the read c.Request.Body length is 0
         //  mac:= c.PostForm("mac")

              b,_=io.ReadAll(c.Request.Body)
              fmt.Println("request body length:%d\n",len(b))

        c.String(200, "Hello %s", c.Param("name"))
    })
    g.Run(":9000")
}

Environment

  • go version:1.19
  • gin version (or commit ref):1.8.1
  • operating system: windows 11 x64

Comment From: mstmdev

Because when you call the c.PostForm function, the c.Request.Body will be read to end, and when call the io.ReadAll again you actually will get an io.EOF error, but io.ReadAll will hide it.

Comment From: qifengzhang007

Because when you call the c.PostForm function, the c.Request.Body will be read to end, and when call the io.ReadAll again you actually will get an io.EOF error, but io.ReadAll will hide it.

thank you very much !