I am trying to get the request header in a mojolicious template. I need to get a custom header. In Flask I would use
{{request.headers['customheader] }}
How do I achieve the same in Mojolicious? There is a Mojo::Message::Request
package.
So are what I have isn't working.
% use Mojo::Message::Request;
% my $req = Mojo::Message::Request->new;
<%= $req->headers->header('customheader') %>
If I only print $req
I get a giant hash.
I have also tried to use
% use Mojo::Headers;
% my $headers = Mojo::Headers->new;
<%= $headers->parse('X-Forwarded-For') %>
Since mojolicious injects the controller object as the local variable $c
and $self
into your view, you can easily access the request headers:
# Your Template-Code
Some-Header: <strong><%= $c->req->headers->header("Some-header") %></strong>
$c->req->headers
is a Mojo::Headers object and contains all headers from the current request.