Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KaustubhPatange opened this issue Apr 19, 2024 · 0 comments
Open

Comments

@KaustubhPatange
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant