Question

Trying to learn Mojolicious here. For the following request, I get 404 when I try to get to

http://hostname:3000/xml

Here is the simple script:

use Mojolicious::Lite;
use Data::Dumper;


get '/xml' => sub {
    my $self  = shift;
    $self->render(xml => "<employees>  
 <employee>  
      <id>1001</id>  
       <name>John Smith</name>  
 </employee>  
 <employee>  
      <id>1002</id>  
       <name>Jane Dole</name>  
 </employee>  
 </employees>"
    );
};

app->start;

This script was adopted from an example for json, which works fine. Not sure why xml doesn't work.

Was it helpful?

Solution

Just need to specify a format

get '/xml' => sub {
    my $self  = shift;

    my $xml = <<'XML';
<employees>
<employee><id>1001</id><name>John Smith</name></employee>
<employee><id>1002</id><name>Jane Dole</name></employee>
</employees>
XML

    $self->render(data => $xml, format => 'xml');
};

Response header equals the following:

Connection: keep-alive
Server: Mojolicious (Perl)
Content-Length: 140
Content-Type: application/xml
Date: Wed, 09 Apr 2014 05:36:05 GMT

200 OK

Could also place the data in a template, of course:

get '/xml' => sub {
    my $self  = shift;

    $self->render('employees', format => 'xml');
};

app->start;

__DATA__

@@ employees.xml.ep
<employees>
<employee><id>1001</id><name>John Smith</name></employee>
<employee><id>1002</id><name>Jane Dole</name></employee>
</employees>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top