# 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](https://pkg.go.dev/github.com/kataras/iris/v12/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/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...

```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/main/_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("/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](https://github.com/kataras/iris/blob/main/_examples/response-writer/transactions/main.go).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://iris-go.gitbook.io/iris/responses/recorder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
