Get HTTP headers

To get the HTTP headers, use the header method of the Mojo::Headers class.

my $x_forwarded_host = $headers->header('X-Forwarded-Host');

The Mojo::Headers class also has a lot of shortcut methods for getting common headers.

my $content_type = $headers->content_type;
my $accept_language = $headers->accept_language;

Below is a sample of Mojolicious::Lite. The Mojo::Message::Request object is fetched by the req method of the Mojolicious::Controller object, and the Mojo::Headers object is fetched by the headers method of the Mojo::Message::Request object.

#Mojolicious::Lite
use Mojolicious::Lite;

get'/' => sub {
  my $self = shift;
  my $x_forwarded_host = $self->req->headers->header('X-Forwarded-Host');
  my $content_type = $self->req->headers->content_type;
  my $accept_language = $self->req->headers->accept_language;
};;

Below is a sample of Mojolicious.

package MyApp::Diary;

use Mojo::Base'Mojolicious::Controller';

sub entry {
  my $self = shift;
  my $x_forwarded_host = $self->req->headers->header('X-Forwarded-Host');
  my $content_type = $self->req->headers->content_type;
  my $accept_language = $self->req->headers->accept_language;
}

Associated Information