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

ctx.ReadForm() return a error struct if give more params. #1298

Closed
wyanlord opened this issue Jul 12, 2019 · 3 comments
Closed

ctx.ReadForm() return a error struct if give more params. #1298

wyanlord opened this issue Jul 12, 2019 · 3 comments

Comments

@wyanlord
Copy link

wyanlord commented Jul 12, 2019

// 参数体
type RequestParams struct {
	A string `form:"a"`
	B string `form:"b"`
	C string `form:"c"`
}

// 请求日志记录
func NewAccessLogMdw() iris.Handler {
	return func(ctx context.Context) {
		var p = RequestParams{}
		ctx.ReadForm(&p)

		fmt.Println(p)

		ctx.Next()
	}
}

When post http://localhost:8080/users?c=5&d=1 with b=3.
The many results are:

{  }
{  }
{  5}
{ 3 5}
{ 3 5}
{ 3 }

If this can skip it without no exists param automatically, it will be better.

@kataras
Copy link
Owner

kataras commented Jul 12, 2019

I don't quite understood, but you can check and skip an error coming from missing value for a struct field at ReadForm by IsErrPath

err := ctx.ReadForm(&visitor)
if err != nil && !iris.IsErrPath(err) /* see: https://github.com/kataras/iris/issues/1157 */ {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.WriteString(err.Error())
}

@samchen945
Copy link

samchen945 commented Jul 20, 2019

Hi, seems I have same issue here.

A very simple model:

type Order struct {
	OrderId string `form:"order_id"`
}

Post handler:

app.Handle("POST", "/", func(ctx context.Context) {
		fmt.Println("Raw form", ctx.FormValues())
		fmt.Println("Raw order_id", ctx.FormValue("order_id"))
		order := model.Order{}
		err := ctx.ReadForm(&order)
		if err != nil && !iris.IsErrPath(err) {
			fmt.Println(err)
			return
		}

		fmt.Println("In model: order_id(", order.OrderId, ")")
		fmt.Println("========")
	})

Postman values (As you might see I passed an extra email value):
image

Did several tests, and there were high chances of failures:

image

Any ideas or any mistake in my code?

@kataras
Copy link
Owner

kataras commented Jul 23, 2019

Fixed by upstream and pushed to v11.2.1 @wyanlord and @samchen945 thanks a lot for the example.

kataras added a commit that referenced this issue Jul 24, 2019
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
Former-commit-id: 7371178a7662a7e55bd1c83a22ae3a4896b449fb
github-actions bot pushed a commit to goproxies/github.com-kataras-iris that referenced this issue Jul 27, 2020
Former-commit-id: fca9db43ad6d78fcf01acf8edeadc12abc34195f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants