Description

I would like to iterate over a PostForm, but ran into several issues while attempting this.

How to reproduce

<form action="/test" method="POST">
  <input type="radio" name="foo" value="a">
  <input type="radio" name="bar" value="b">
</form>
router.POST("/test", func test(ctx *gin.Context) {
    for name, value := range ctx.Request.PostForm {
        fmt.Println(name, value)
    }
})

The code above prints nothing. I found out that this is due to the POST form not being parsed, unless one tries to get a specific value: https://github.com/gin-gonic/gin/blob/7d8fc1563b4e1b4229e61c2fe4c9e31ce13ace7d/context.go#L528-L547

This fixes it:

router.POST("/test", func test(ctx *gin.Context) {
    ctx.GetPostForm("") // To trigger parsing
    for name, value := range ctx.Request.PostForm {
        fmt.Println(name, value)
    }
})

It would be nice if it was possible to trigger the parsing manually, or have some sort of iterator over the values. Perhaps just making initFormCache() public? The worst case is that someone calls it without knowing what it does, but it doesn't really break anything if they do.

Comment From: zanmato

Look at the documentation of PostForm, you have to call ParseForm first, then it'll work

// This field is only available after ParseForm is called.

Comment From: benstigsen

I forgot that this issue was open, and yes that is true.