kataras / kataras/iris

[Question] Can not bind parameter of struct using ReadBody

Open
#2,371 0 comments 0 reactions 1 assignee Claimed by @kataras View on GitHub
Dominant language
Go
Stars
25.6k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
I use iris as a simple web server and found a problem in parameter binding. When I send two int64 parameters with Get method, the second parameter will not bind to my struct. This problem may not the regular of bug description but occurred when I debug and step into the variable.

**To Reproduce**
Steps to reproduce the behavior:
```
// 1. My struct is defined as:
type GTClip struct {
Id int64 `gorm:"column:id" json:"id"`
Clip string `gorm:"column:clip" json:"clip"`
Scene string `gorm:"column:scene" json:"scene"`
Path string `gorm:"column:path" json:"path"`
SetId int64 `gorm:"column:set_id" json:"set_id"`
Deleted int `gorm:"column:deleted" json:"-"`
}
// 2. the route and function implemented as:
gt := app.Party("/gt")
{
gt.Use(iris.Compression)
clip := gt.Party("/clip")
{
// ... other unrelated method
clip.Get("/list", func (ctx iris.Context) {
var (
gtClipReq = &model.GTClip{}
err error
)
if err := ctx.ReadBody(gtClip); err != nil { // step point 1
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
if gtClip.SetId, err = ctx.URLParamInt64("set_id"); err != nil { // step point 2
ctx.StopWithError(iris.StatusBadRequest, err)
return
}
ctx.JSON(iris.Map{
"code": 0,
"err": nil,
})
})
}
}
```
3. launch server and send request: http://127.0.0.1:8080/gt/clip/list?id=11&set_id=12
**Expected behavior**
The struct GTClip menber of Id and SetId should be bind as 11 and 12 respectively, with above requests. But until called function "ctx.URLParamInt64("set_id")", the member SetId is not binded the expected value 12. As comment at "step point 1".
After calling the ctx.URLParamInt64("set_id"), SetId is set to 12. As comment at "step point 2".
Another experiment is that if I add a parameter scene, member of Scene will be correctly binded! eg: http://127.0.0.1:8080/gt/clip/list?id=11&set_id=11&scene=turn
**Screenshots**
![image](https://github.com/kataras/iris/assets/31685313/f23244f5-f070-4198-b7cd-02ac6f3fe402)
![image](https://github.com/kataras/iris/assets/31685313/ef9f40ca-899d-44c8-acf3-8ff01e2d6d0b)

**Desktop (please complete the following information):**
- OS: macOs 13.2 (22D49)

**iris.Version**
- v12.2.8

Please make sure the bug is reproducible over the `main` branch:
YES, the version I updated is v12.2.11-0.20240122133509-ddda4f699854, still has same problem

**Additional context**
I think there might be some misunderstand when I use ctx.ReadBody method. In fact I can't remember such many parameter binding methods and always use ctx.ReadBody instead. Am I right?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.