From fffdbf67a3a5a4a9e6b060ad177e2e6727327da9 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 9 Aug 2019 23:10:16 +0300 Subject: [PATCH] add response recorder section --- Home.md | 1 + Response-recorder.md | 104 +++++++++++++++++++++++++++++++++++++++++++ _Sidebar.md | 1 + 3 files changed, 106 insertions(+) create mode 100644 Response-recorder.md diff --git a/Home.md b/Home.md index d9bd727..543af80 100644 --- a/Home.md +++ b/Home.md @@ -23,6 +23,7 @@ This wiki is the main source of documentation for **developers** working with (o * [[Context Methods|Routing-context-methods]] * [[API Versioning]] * [[Content Negotiation]] +* [[Response Recorder]] * [[HTTP Referer]] * [[Request Authentication]] * [[URL Query Parameters]] diff --git a/Response-recorder.md b/Response-recorder.md new file mode 100644 index 0000000..3e9afc1 --- /dev/null +++ b/Response-recorder.md @@ -0,0 +1,104 @@ +A response recorder is an Iris specific `http.ResponseWriter` which records the sent body, status code and headers that you can manipulate at any handler inside a route's request handlers chain. + +1. Call `Context.Record()` before send data. +2. The `Context.Recorder()` returns a [ResponseRecorder](https://godoc.org/github.com/kataras/iris/context#ResponseRecorder). Its methods can be used to manipulate or retrieve the response. + +The `ResponseRecorder` type contains the standard Iris ResponseWriter methods plus the following methods. + +Body **returns** the body tracked from the writer so far. +Do not use this for edit. + +```go +Body() []byte +``` + +Use this to clear the body. + +```go +ResetBody() +``` + +Use `Write/Writef/WriteString` to stream write and `SetBody/SetBodyString` to **set** body instead. + +```go +Write(contents []byte) (int, error) + +Writef(format string, a ...interface{}) (n int, err error) + +WriteString(s string) (n int, err error) + +SetBody(b []byte) + +SetBodyString(s string) +``` + +Reset headers to their original state, before `Context.Record` call. + +```go +ResetHeaders() +``` + +Clear all headers. + +```go +ClearHeaders() +``` + +Reset resets the response body, headers and the status code header. + +```go +Reset() +``` + +## Example + +Record operation log in global Interceptor. + +```go +package main + +import "github.com/kataras/iris" + +func main() { + app := iris.New() + + // start record. + app.Use(func(ctx iris.Context) { + ctx.Record() + ctx.Next() + }) + + // collect and "log". + app.Done(func(ctx iris.Context) { + body := ctx.Recorder().Body() + + // Should print success. + app.Logger().Infof("sent: %s", string(body)) + }) +} +``` + +Register the routes... + +```go +app.Get("/save", func(ctx iris.Context) { + ctx.WriteString("success") + ctx.Next() // calls the Done middleware(s). +}) +``` + +Or to remove the need of `ctx.Next` in your main handlers, modify the Iris handlers [execution rules](https://github.com/kataras/iris/blob/master/_examples/mvc/middleware/without-ctx-next/main.go) as follows. + +```go +// It applies per Party and its children, +// therefore, you can create a routes := app.Party("/path") +// and set middlewares, their rules and the routes there as well. +app.SetExecutionRules(iris.ExecutionRules{ + Done: iris.ExecutionOptions{Force: true}, +}) + +// [The routes...] +app.Get("/save", func(ctx iris.Context) { + ctx.WriteString("success") +}) +``` diff --git a/_Sidebar.md b/_Sidebar.md index 2c193ee..46a160f 100644 --- a/_Sidebar.md +++ b/_Sidebar.md @@ -18,6 +18,7 @@ * [[Context Methods|Routing-context-methods]] * [[API Versioning]] * [[Content Negotiation]] +* [[Response Recorder]] * [[HTTP Referer]] * [[Request Authentication]] * [[URL Query Parameters]]