I'm trying to upload a file to my server with additional information attached to it as json.
I can upload a file alone with gin doing :
file, err := c.FormFile("file")
if err != nil {
return error
}
But i can't figure out how to pass the file AND the json at the same time.
type FileJson struct {
Name string `json:"name" binding:"required"`
Description string `json:"description"`
fileData *multipart.FileHeader `form:"file"`
}
func UploadFile(c *gin.Context) {
var testmultipart fileJSON
err := c.Bind(&testmultipart)
if err != nil {
return err
}
c.JSON(http.StatusOK, gin.H{"status": http.StatusText(http.StatusOK), "data": "xx"})
}
This doesn't work and doesn't unparse the json data into the struct ( either fialing on the required tag or just having empty field inside the struct )
Does anybody knows how to do that so that i can send a file + json in on request ?
One solution would be to encode the file to base64 and unparse it as a []byte but that shouldn't be necessary and make the request larger so i want to do it with mime/multipart.