-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
respect the WithoutBodyConsumptionOnUnmarshal on 'ctx.ReadForm' and '…
…ctx.FormValues' and on the new 'ctx.GetBody' method helper as requested at: #1297
- Loading branch information
Showing
3 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/kataras/iris" | ||
) | ||
|
||
func main() { | ||
app := iris.New() | ||
|
||
app.Post("/", logAllBody, logJSON, logFormValues, func(ctx iris.Context) { | ||
body, err := ctx.GetBody() | ||
if err != nil { | ||
ctx.Writef("error while reading the requested body: %v", err) | ||
return | ||
} | ||
|
||
if len(body) == 0 { | ||
ctx.WriteString(`The body was empty | ||
or iris.WithoutBodyConsumptionOnUnmarshal option is missing from app.Run. | ||
Check the terminal window for any queries logs.`) | ||
|
||
} else { | ||
ctx.WriteString("OK body is still:\n") | ||
ctx.Write(body) | ||
} | ||
}) | ||
|
||
// With ctx.UnmarshalBody, ctx.ReadJSON, ctx.ReadXML, ctx.ReadForm, ctx.FormValues | ||
// and ctx.GetBody methods the default golang and net/http behavior | ||
// is to consume the readen data - they are not available on any next handlers in the chain - | ||
// to change that behavior just pass the `WithoutBodyConsumptionOnUnmarshal` option. | ||
app.Run(iris.Addr(":8080"), iris.WithoutBodyConsumptionOnUnmarshal) | ||
} | ||
|
||
func logAllBody(ctx iris.Context) { | ||
body, err := ctx.GetBody() | ||
if err == nil && len(body) > 0 { | ||
ctx.Application().Logger().Infof("logAllBody: %s", string(body)) | ||
} | ||
|
||
ctx.Next() | ||
} | ||
|
||
func logJSON(ctx iris.Context) { | ||
var p interface{} | ||
if err := ctx.ReadJSON(&p); err == nil { | ||
ctx.Application().Logger().Infof("logJSON: %#+v", p) | ||
} | ||
|
||
ctx.Next() | ||
} | ||
|
||
func logFormValues(ctx iris.Context) { | ||
values := ctx.FormValues() | ||
if values != nil { | ||
ctx.Application().Logger().Infof("logFormValues: %v", values) | ||
} | ||
|
||
ctx.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters