We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Bind a a non mandatory file parameter will report an error.
package main import ( "fmt" "github.com/gin-gonic/gin" "mime/multipart" "net/http" ) var html = `<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>File binding</title> </head> <body> <h1>Bind file with fields</h1> <form action="/upload" method="post" enctype="multipart/form-data"> Name: <input type="text" name="name"><br> Email: <input type="email" name="email"><br> File: <input type="file" name="file"><br><br> <input type="submit" value="Submit"> </form> </body>` type BindFile struct { Name string `form:"name" binding:"required"` Email string `form:"email" binding:"required"` File *multipart.FileHeader `form:"file" ` } func main() { router := gin.Default() // Set a lower memory limit for multipart forms (default is 32 MiB) router.GET("/", func(ctx *gin.Context) { ctx.Header("Content-Type", "text/html") ctx.String(200, html) }) router.POST("/upload", func(c *gin.Context) { var bindFile BindFile if err := c.ShouldBind(&bindFile); err != nil { // will report an error when file is not uploaded. // Can we set it a nil pointer by default ? c.String(http.StatusBadRequest, fmt.Sprintf("err: %s", err.Error())) return } // TODO::... c.String(http.StatusOK, fmt.Sprintf("File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, bindFile.Name, bindFile.Email)) }) router.Run(":8080") }
$ curl 'http://localhost:8080/upload' \ -H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryK7Skf1qO5bW9JEX0' \ --data-raw $'------WebKitFormBoundaryK7Skf1qO5bW9JEX0\r\nContent-Disposition: form-data; name="name"\r\n\r\n\r\n------WebKitFormBoundaryK7Skf1qO5bW9JEX0\r\nContent-Disposition: form-data; name="email"\r\n\r\n\r\n------WebKitFormBoundaryK7Skf1qO5bW9JEX0\r\nContent-Disposition: form-data; name="file"; filename=""\r\nContent-Type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundaryK7Skf1qO5bW9JEX0--\r\n'
err: unexpected end of JSON input
The text was updated successfully, but these errors were encountered:
Error Line is : #L239
can we set it nil by default or using assertions like: time.Time ,
Because we used JSON for him The unmarshal function causes its parsing to fail, and in this scenario, we should not do so
Sorry, something went wrong.
fix: binding error while not upload file (gin-gonic#3819)
493b3a6
3e3598d
8a84480
fix(binding): binding error while not upload file (#3819) (#3820)
3dc1cd6
Co-authored-by: zhangmj <zhangmj1@dustess.com>
Successfully merging a pull request may close this issue.
Description
Bind a a non mandatory file parameter will report an error.
How to reproduce
Expectations
Actual result
Environment
The text was updated successfully, but these errors were encountered: