Recorder

A response recorder is one of the Iris specific http.ResponseWriter. It records the response body, status code and headers that you can manipulate at any handler inside a route's handlers chain.

  1. Call Context.Record() before send data.

  2. The Context.Recorder() returns a 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.

Body() []byte

Use this to clear the body.

ResetBody()

Use Write/Writef/WriteString to stream write and SetBody/SetBodyString to set body instead.

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.

ResetHeaders()

Clear all headers.

ClearHeaders()

Reset resets the response body, headers and the status code header.

Reset()

Example

Record operation log in global Interceptor.

package main

import "github.com/kataras/iris/v12"

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.
        ctx.Application().Logger().Infof("sent: %s", string(body))
    })
}

Register the routes...

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 as follows.

// 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("/data", func(ctx iris.Context) {
    ctx.JSON(data)
})

In addition to that, Iris provides a comprehensive API for Transactions. Learn more about it by running an example.

Last updated