Iris: ReadForm can't skip unmapped parameter automatically

Created on 20 Dec 2018  ·  5Comments  ·  Source: kataras/iris

hello
My project use ReadForm to parse request body.but if I submit an unmapped parameter to that API.

for example:
//server
type Test struct{
Name string form:"name"
}
...
test:=Test{}
ctx.ReadForm(&test)

//client request
http.POST("/xx/test",{
"name":"test",
"testkey":"xxx"
})
//end

I got the error msg like this:

_while trying to read formam: not found the field "testkey" in the path "testkey" from the request body. Trace %!s(MISSING)_

But my struct define nothing about "testkey". maybe it's helpfully to skip unmapped parameters automatically.

thanks

implemented feature

Most helpful comment

Done @shenkaige , the updated example looks like this (note the new IsErrPath):

    app.Post("/form_action", func(ctx iris.Context) {
        visitor := Visitor{}
        err := ctx.ReadForm(&visitor)
        if err != nil && !iris.IsErrPath(err) { // it will ignore error if client sends unexpected fields.
            ctx.StatusCode(iris.StatusInternalServerError)
            ctx.WriteString(err.Error())
        }

        ctx.Writef("Visitor: %#v", visitor)
    })

All 5 comments

So you send "name" and "testkey" but server expects only "name" and that gives you an error. This is useful, that's no the problem, I think the actual solution is to give the error a "context" that you can check and skip on the handler state. Sounds good to you, should I implement something like this to sovle the issue?

So you send "name" and "testkey" but server expects only "name" and that gives you an error. This is useful, that's no the problem, I think the actual solution is to give the error a "context" that you can check and skip on the handler state. Sounds good to you, should I implement something like this to sovle the issue?

Thanks for you solution.you are right 👍

Done @shenkaige , the updated example looks like this (note the new IsErrPath):

    app.Post("/form_action", func(ctx iris.Context) {
        visitor := Visitor{}
        err := ctx.ReadForm(&visitor)
        if err != nil && !iris.IsErrPath(err) { // it will ignore error if client sends unexpected fields.
            ctx.StatusCode(iris.StatusInternalServerError)
            ctx.WriteString(err.Error())
        }

        ctx.Writef("Visitor: %#v", visitor)
    })

nice,thank you very much.🎉

You are very welcomed!

Was this page helpful?
0 / 5 - 0 ratings