CC: | Andrew Solomon <andrew [...] illywhacker.net> |
Subject: | Dancer-Plugin-REST v0.07 |
Date: | Tue, 17 Jul 2012 22:35:59 +0100 |
To: | bug-Dancer-Plugin-REST [...] rt.cpan.org |
From: | Andrew Solomon <andrew [...] illywhacker.net> |
It appears that the first call to a POST route fails. Below are two
examples of the server, where the second one (which doesn't use
Dancer::Plugin::REST) works fine.
Context Info
=========
Dancer (1.3097)
Dancer::Plugin::REST (0.07)
$ perl -v
This is perl 5, version 12, subversion 4 (v5.12.4) built for
x86_64-linux-gnu-thread-multi
(with 45 registered patches, see perl -V for more detail)
$ uname -a
Linux gym 3.0.0-12-server #20-Ubuntu SMP Fri Oct 7 16:36:30 UTC 2011
x86_64 x86_64 x86_64 GNU/Linux
======= The client ========
#!/usr/bin/perl
use strict;
use warnings;
use URI;
use LWP::UserAgent;
use JSON;
use Data::Dump qw/pp/;
use feature qw/say/;
# uri
my $dns_uri = URI->new();
$dns_uri->scheme('http');
$dns_uri->authority('localhost');
$dns_uri->port (3000);
$dns_uri->path('hw.json');
# request
my $req = HTTP::Request->new (POST => $dns_uri);
$req->content_type('application/json');
my $rh_post_data = { foo => 'bar', yin => 'yang' };
$req->content(to_json($rh_post_data));
say 'Request:';
say pp($req);
# LWP
my $lwp = LWP::UserAgent->new;
my $res = $lwp->request( $req );
unless ($res->is_success) {
say STDERR 'Error: '. $res->status_line;
}
say 'Response:';
say pp $res->content;
========== Client output (same for both servers) ==========
andrew@gym:~/tmp/dancerbug $ ./client.pl
Request:
bless({
_content => "{\"yin\":\"yang\",\"foo\":\"bar\"}",
_headers => bless({ "content-type" => "application/json" }, "HTTP::Headers"),
_method => "POST",
_uri => bless(do{\(my $o = "http://localhost:3000/hw.json")},
"URI::http"),
}, "HTTP::Request")
Response:
"Hello world"
========= server.pl using Dancer::Plugin::REST =================
#!/usr/bin/perl
use Dancer;
use Dancer::Plugin::REST;
use Data::Dump qw/pp/;
set logger => 'console';
set log => 'debug';
set show_errors => 1;
set port => 3000;
prepare_serializer_for_format;
post '/hw.:format' => sub {
debug ("parameters: ".pp(params));
return 'Hello world';
};
dance;
============= Incorrect server output - first call doesn't catch
{'yin': 'yang', 'foo': 'bar'} ======
$ ./server.pl.bug
Show quoted text
>> Dancer 1.3097 server 7982 listening on http://0.0.0.0:3000
>> Dancer::Plugin::REST (0.07)
== Entering the development dance floor ...
[7982] debug @0.007872> [hit #1]parameters: ("format", "json") in
./server.pl.bug l. 21
[7982] debug @0.001058> [hit #2]parameters: ("format", "json", "yin",
"yang", "foo", "bar") in ./server.pl.bug l. 21
======= server.pl without Dancer::Plugin::REST ============
#!/usr/bin/perl
use Dancer;
use Data::Dump qw/pp/;
set logger => 'console';
set log => 'debug';
set show_errors => 1;
set port => 3000;
set serializer => 'JSON';
post '/hw.json' => sub {
debug ("parameters: ".pp(params));
return 'Hello world';
};
========= Correct server output ================
$ ./server.pl
Show quoted text>> Dancer 1.3097 server 7996 listening on http://0.0.0.0:3000
>> Dancer::Plugin::REST (0.07)
== Entering the development dance floor ...
[7996] debug @0.000746> [hit #1]parameters: ("yin", "yang", "foo",
"bar") in ./server.pl l. 18
[7996] debug @0.000712> [hit #2]parameters: ("yin", "yang", "foo",
"bar") in ./server.pl l. 18