You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can this bug be reproduced with the latest release?
Option Yes
What did you do?
When handling a file upload using ghttp.UploadFile, if the file field in the multipart request is an empty string (""), the server panics instead of triggering the validation rule.
Here is a minimal reproducible example:
package main
import (
"context""fmt""time""github.com/gogf/gf/v2/frame/g""github.com/gogf/gf/v2/net/ghttp"
)
typeReqstruct {
g.Meta`method:"post" mime:"multipart/form-data"`File*ghttp.UploadFile`v:"required" type:"file"`// File is required
}
typeResstruct{}
funcmain() {
s:=g.Server()
s.BindMiddlewareDefault(ghttp.MiddlewareHandlerResponse)
s.BindHandler("/upload", func(ctx context.Context, req*Req) (res*Res, errerror) {
return
})
s.SetDumpRouterMap(false)
s.SetAccessLogEnabled(false)
s.SetErrorLogEnabled(false)
s.Start()
defers.Shutdown()
time.Sleep(100*time.Millisecond)
client:=g.Client()
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
content:=client.PostContent(context.Background(), "/upload", g.Map{
"file": "",
})
fmt.Println(content)
// {"code":68,"message":"reflect.Value.Convert: value of type string cannot be converted to type ghttp.UploadFile","data":null}
}
What did you see happen?
When the file field is empty (""), the server crashes with the following panic:
reflect.Value.Convert: value of type string cannot be converted to type ghttp.UploadFile
This happens instead of triggering the expected validation error.
What did you expect to see?
The framework should trigger validation and return an appropriate "file is required" error instead of panicking.
Expected behavior:
The request should fail with a 400 Bad Request response.
The error message should be similar to "file is required" due to the v:"required" validation rule.
The framework should not attempt to convert an empty string to ghttp.UploadFile, preventing the panic.
The text was updated successfully, but these errors were encountered:
MHanL
added
the
bug
It is confirmed a bug, but don't worry, we'll handle it.
label
Mar 12, 2025
This is because client.PostContent determines whether to set the Content-Type to multipart/form-data according to whether the value of the parameter starts with @file:.
Go version
go version go1.24.0 windows/amd64
GoFrame version
v2.8.3
Can this bug be reproduced with the latest release?
Option Yes
What did you do?
When handling a file upload using
ghttp.UploadFile
, if thefile
field in the multipart request is an empty string (""
), the server panics instead of triggering the validation rule.Here is a minimal reproducible example:
What did you see happen?
When the
file
field is empty (""
), the server crashes with the following panic:This happens instead of triggering the expected validation error.
What did you expect to see?
The framework should trigger validation and return an appropriate "file is required" error instead of panicking.
Expected behavior:
400 Bad Request
response."file is required"
due to thev:"required"
validation rule.ghttp.UploadFile
, preventing the panic.The text was updated successfully, but these errors were encountered: