gin-gonic / gin

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Home Page:https://gin-gonic.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multipart form-data request with empty key for single file upload

KaustubhPatange opened this issue · comments

We're are migrating one of our micro-services written in NodeJS Javascript which handles all things related to upload into Go, thinking to use gin framework. However I've noticed a few things when doing a multipart upload.

Following is the basic code implementation

package main

import (
	"fmt"
	"net/http"

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

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

	r.POST("/upload", func(c *gin.Context) {
		c.Request.ParseMultipartForm(32 << 20)
		fm, err := c.MultipartForm()
		if err != nil {
			c.String(http.StatusBadRequest, "Bad request, no file uploaded")
			return
		}

		fmt.Println(fm.File)

		c.String(http.StatusOK, "OK")
	})

	r.Run(":8081")
}
  • For curl --location '0.0.0.0:8081/upload' \ --form 'file=@"/Users/name/Downloads/test.pdf"' ,
    • It prints map[file:[0x14000194ea0]]
  • For curl --location '0.0.0.0:8081/upload' \ --form '=@"/Users/devel/Downloads/Profile.pdf"',
    • It prints map[]

To my surprise it seems that when the "key" is not provided it fails to parse the form body. We did not face this issue with our current implementation in NodeJs Fastify framework, where request.file() gives you the first file in the multipart request.

Is there a way in Gin which has such similar effect, maybe grouping by form boundary?