Search code examples
perlrestmojolicious

REST Server: Mojolicious truncating stash values


I'm fairly new to Mojolicious and creating REST services. I am trying to write a REST service to query an asset database.

An example URL:

http://test.example.com:3000/test/asset/web01.example.local

That gets routed to the following logic:

get '/test/asset/:node' => sub {
        my $self  = shift;
        my $node = $self->stash('node');
        my $sql = qq {
            SELECT id, name, type, location 
            FROM inventory 
            WHERE name = ?
        };
        my $cursor = $self->db->prepare($sql);
        $cursor->execute($node,$node);
        my $record = $cursor->fetchrow_hashref;
        $cursor->finish;
        $self->db_disconnect;

        say $self->stash('node');
        return $self->render(json => $record);
};

The "say" statement reveals that .example.local is being truncated:


[Wed Aug 27 21:24:05 2014] [debug] GET "/test/asset/web01.example.local".
[Wed Aug 27 21:24:05 2014] [debug] Routing to a callback. 
web01
[Wed Aug 27 21:24:05 2014] [debug] 200 OK (0.004076s, 245.339/s).

How do I get my service to accept an string what contains dot's it? If this is not possible, what would be a way I can use a GET request and pass an FQDN (The only thing I can use to query 2 independent DB's) and return a result?


Solution

  • I think you have to change the type of place-holder from generic to relaxed.

    get '/test/asset/*node' => sub {
       # your sub continues here ...
    

    See also the Mojolicious Guide Routing