Headers
Content-Type Requested: *
1
// Register the route...
2
app.Get("/", handler)
3
4
func handler(ctx iris.Context) {
5
requestID := ctx.GetHeader("X-Request-Id")
6
authentication := ctx.GetHeader("Authentication")
7
}
Copied!
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.
1
type myHeaders struct {
2
RequestID string `header:"X-Request-Id,required"`
3
Authentication string `header:"Authentication,required"`
4
}
Copied!
Now, the handler which reads the request headers and binds them to a "myHeaders" value.
1
func handler(ctx iris.Context) {
2
var hs myHeaders
3
if err := ctx.ReadHeaders(&hs); err != nil {
4
ctx.StopWithError(iris.StatusInternalServerError, err)
5
return
6
}
7
8
ctx.JSON(hs)
9
}
Copied!
Request
1
curl -H "x-request-id:373713f0-6b4b-42ea-ab9f-e2e04bc38e73" -H "authentication: Bearer my-token" \
2
http://localhost:8080
Copied!
Result
1
{
2
"RequestID": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
3
"Authentication": "Bearer my-token"
4
}
Copied!
Last modified 1yr ago
Export as PDF
Copy link
Contents
Bind