Hi Daniel,

Thanks for your reply. I am doing nothing myself with the content length, indeed I am using HTTP::Request::Common to handle everything.

Here's the test script inline:


#!/usr/bin/env perl

package MyApp;

use strict;
use warnings;
use Dancer2;
use Data::Dumper;

set serializer => 'JSON';

any '/ok' => sub {
    return {};
};

any '/nok' => sub {
    my $a = request->content;
    #print STDERR Dumper $a;
    #print STDERR Dumper { params('body') };
    return {};
};

# dance();


package main;

use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use JSON;

my $app = MyApp->to_app;
isa_ok( $app, 'CODE' );

test_psgi $app, sub {
    my $cb = shift;

    my $res = $cb->( GET '/ok' );
    ok($res->code == 200, "GET route without request->content call");

    $res = $cb->( GET '/nok' );
    ok($res->code == 200, "GET route with request->content call");

    my $json_data = { foo => 'bar' };
    $res = $cb->(
        POST '/ok',
        'Content-Type'    => 'application/json',
        Content => JSON::to_json($json_data),
    );
ok($res->code == 200, "JSON POST to route without request->content call");

    $res = $cb->(
        POST '/nok',
        'Content-Type'    => 'application/json',
        Content => JSON::to_json($json_data),
    );
    # this test fails.
    ok($res->code == 200, "JSON POST to route with request->content call");

};

done_testing;


1;



On 22-07-16 13:44, Daniel Perrett wrote:
I'm afraid I didn't get the attachment, so apologies if this guess is out, but 
are you setting the content-length on your HTTP::Request? If not, have a look 
at how HTTP::Request::Common does it - 
https://metacpan.org/source/ETHER/HTTP-Message-6.11/lib/HTTP/Request/Common.pm#L97
 - (or indeed just use HTTP::Request::Common itself).

Daniel

-----Original Message-----
From: dancer-users [mailto:[email protected]] On Behalf Of Lennart 
Hengstmengel
Sent: 22 July 2016 12:38
To: [email protected]
Subject: [dancer-users] "Route exception: Bad Content-Length: maybe client 
disconnect?" with JSON POST and request->content

Hi all,

I'm running into an issue that looks like a bug, but I'm unsure where it comes 
from. Could be in Dancer2, but maybe in Plack::Request, or even in Plack::Test, 
HTTP::Request, or somewhere else. I'm kinda lost in the woods. I was hoping 
that a mind greater than mine can shed some light on this.

I get an error "Route exception: Bad Content-Length: maybe client disconnect? (xx 
bytes remaining)" where xx is the total content length of the request body.
But only under the following conditions:

- (valid) JSON POST to a Dancer2 app
- the app is configured with serializer = JSON
- in the route there's a call to: request->content
- using Plack::Test and HTTP::Request

Attached is a minimal test to demonstrate the issue. Test no 5 fails.

I cannot reproduce this issue when using f.e. curl, this call works without 
errors:

      curl --data-binary '{ "foo": "123" }' --header 'Content-type:
application/json' http://localhost:3000/nok

I am using the latest version of Dancer (0.200003) and also latest CPAN 
versions of Plack and HTTP::Request.

Any insights?

Thanks,
Lennart




_______________________________________________
dancer-users mailing list
[email protected]
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users


_______________________________________________
dancer-users mailing list
[email protected]
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users

Reply via email to