Headers

Content-Type Requested: *

// Register the route...
app.Get("/", handler)

func handler(ctx iris.Context) {
    requestID := ctx.GetHeader("X-Request-Id")
    authentication := ctx.GetHeader("Authentication")
}

To get all request headers use ctx.Request().Header instead.

Bind

The Context.ReadHeaders(ptr interface{}) error is the method which binds request headers to a custom value.

Let's create our Go structure.

type myHeaders struct {
	RequestID      string `header:"X-Request-Id,required"`
	Authentication string `header:"Authentication,required"`
}

Now, the handler which reads the request headers and binds them to a "myHeaders" value.

func handler(ctx iris.Context) {
    var hs myHeaders
    if err := ctx.ReadHeaders(&hs); err != nil {
        ctx.StopWithError(iris.StatusInternalServerError, err)
        return
    }

    ctx.JSON(hs)
}

Request

Result

Last updated

Was this helpful?