goravel: 🐛 [Bug] ctx.Request().Bind() get EOF
Before feedback (在反馈之前)
- I’ve searched the existing Issues, Discussions and Google (我已经搜索了现有的 Issues, Discussions 和 Google)
- The problem can be stably reproduced (这个问题可以被稳定复现)
- The problem is generated after upgrading (问题是在升级之后产生的)
Operating System (操作系统)
Linux
Golang Version (Go 版本)
1.21.x
Goravel Version (Goravel 版本)
1.13.5
Describe The Problem (描述问题)
ctx.Request().Bind() always get EOF error. does it require all fields of struct to be added in the request json ?
Reproduction Code (复现代码或截图)
request json:
{
"username" : "bc",
"password" : "123456",
"nickname" : "烟火",
"email": "bc@a.b"
}
go struct definition:
type User struct {
*orm.Model
Username string `form:"username" json:"username" gorm:"column:username"`
Password string `form:"password" json:"password" gorm:"column:password"`
Status int `form:"status" json:"status" gorm:"column:status"`
NickName string `form:"nickName" json:"nickName" gorm:"column:nick_name"`
AvatarURL string `form:"avatarURL" json:"avatarURL" gorm:"column:avatar_url"`
Sex int `form:"sex" json:"sex" gorm:"column:sex"`
Email string `form:"email" json:"email" gorm:"column:email"`
MobileCountry string `form:"mobileCountry" json:"mobileCountry" gorm:"column:mobile_country"`
Mobile string `form:"mobile" json:"mobile" gorm:"column:mobile"`
SelfIntro string `form:"selfIntro" json:"selfIntro" gorm:"column:self_intro"`
*orm.SoftDeletes
}
The code I used to bind:
ctx.Request().Bind(&user)
I’ve confirmed that I can get the request param via:
ctx.Request().Input("username", "")
The doc is simple: https://www.goravel.dev/the-basics/request.html#json-form-bind-struct
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 26 (15 by maintainers)
@kuchaguangjie I noticed you were using
err = ctx.Request().Bind(&dummy)
, it should be changed toerr = validator.Bind(&dummy)
, because therequest.Body
had been read. I’ll check if we can usectx.Request().Bind
in this case, but it’s not a bug, you should change it for now.See https://github.com/goravel/fiber
All you need is to add a validation like my above comment before
bind()
.Here is the register() method of user controller:
It works now, but if you uncomment the validation part, it will get
EOF
error.The user model I already passed as above, it doesn’t matter actually. A model with a single field that do both validation & bind can reveal the error.
I can’t reproduce it, can you provide a minimal reproduction example?