Skip to content

Text Response

Learn how to send text responses in Iris.

Basic Text Response

go
package main

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

func main() {
    app := iris.New()

    app.Get("/text", func(ctx iris.Context) {
        ctx.Text("Hello World")
    })

    app.Get("/formatted", func(ctx iris.Context) {
        name := "John"
        age := 25
        ctx.Writef("Name: %s, Age: %d", name, age)
    })

    app.Listen(":8080")
}

Content Type

go
app.Get("/custom", func(ctx iris.Context) {
    ctx.ContentType("text/plain; charset=UTF-8")
    ctx.WriteString("Custom content type")
})

Status Code

go
app.Get("/status", func(ctx iris.Context) {
    ctx.StatusCode(iris.StatusOK)
    ctx.Text("Success!")
})

Best Practices

  1. Content Type:

    • Set appropriate charset
    • Use correct MIME type
    • Handle encoding properly
    • Consider client needs
    • Document requirements
  2. Performance:

    • Optimize response size
    • Enable compression
    • Cache when possible
    • Monitor response times
    • Handle timeouts
  3. Error Handling:

    • Set proper status codes
    • Provide clear messages
    • Log errors appropriately
    • Handle edge cases
    • Validate input
  4. Security:

    • Sanitize output
    • Validate input
    • Set security headers
    • Monitor responses
    • Handle sensitive data

Built with excellence by Hellenic Development, delivering enterprise-grade solutions.