Comment on page
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.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
curl -H "x-request-id:373713f0-6b4b-42ea-ab9f-e2e04bc38e73" -H "authentication: Bearer my-token" \
http://localhost:8080
Result
{
"RequestID": "373713f0-6b4b-42ea-ab9f-e2e04bc38e73",
"Authentication": "Bearer my-token"
}
Last modified 8mo ago