Whenever you have a file field included in your struct the behaviour of the bind methods behaves in various cases not like expected.

type Entity struct {
    Image    *multipart.FileHeader `form:"image"`
    Tags    []string `form:"tags[]"`
}

I don't know why currently but I get several times a "unexpected end of JSON input" error on calling "c.ShouldBind(&form)".

Tags are not bound at all to the struct field. The array interpolation seems not work.


Btw: An ArrayOfType to field mapping (like: element[][id], element[][name]) is missing. This is confusing because the most HTTP post mapping algos in other languages and frameworks can handle this. - It's the typical use case to edit a list of elements via a simple form without using JS/ajax etc. without creating a JSON post request. - A form should allow upload multiple file fields with a description to the server. This is not possible in the current implementation, and file uploads requires a formdata encoded post. Currently it's only solvable by multiple request if you want use the binding functionality without writing extra boilerplate code.


gin 1.4 golang 1.12.5

Comment From: thinkerou

➜  ~ cat x.go
package main

import (
    "mime/multipart"

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

type MailUpload struct {
    Mail []string `form:"mail[]" binding:"required"`
    Attachments *multipart.FileHeader `form:"attachements" binding:"required"`
}

func main() {
    r := gin.Default()

    r.POST("/post/file", func(c *gin.Context) {
        var p MailUpload
        err := c.ShouldBind(&p)
        if err != nil {
            c.JSON(400, gin.H{"msg": "error"})
            return
        }
        println(p.Mail[0])
        println(p.Mail[1])
        c.JSON(200, gin.H{"msg": "OK"})
    })

    r.Run()
}

postman: Gin Unexcepted behaviour if file field present

It's OK.

Comment From: bananasmall

yes i have same problem when the file is optional post not required please any solution here?