http://qs321.pair.com?node_id=1158369

ForeverLearning has asked for the wisdom of the Perl Monks concerning the following question:

This is how it's supposed to work, isn't it ?
my %headerArray = ( "Content-Length"=>2345 ); http_request "PUT" => $url, headers=>%headerArray, body=>$content, sub {do stuff here};

The problem I'm having is that the request goes through and the other side says "411. The request must be chunked or have a content length." The content size definitley matches the size of the body being sent.

I can't see a simple way to turn on tracing in AnyEvent::HTTP either, which isn't helping my debugging.

Replies are listed 'Best First'.
Re: AnyEvent::HTTP not sending ContentLength ?
by 1nickt (Canon) on Mar 20, 2016 at 19:29 UTC

    The docs for AnyEvent::HTTP (which I haven't used) show the headers being passed in to http_request() as a reference to a hash, not the hash itself. Try:

    my $headers = { Content-Length => 2345, }; http_request 'PUT' => $url, 'headers' => $headers, ...

    I can't see a simple way to turn on tracing in AnyEvent::HTTP either, which isn't helping my debugging.

    Maybe setting $ENV{ PERL_ANYEVENT_VERBOSE } see http://search.cpan.org/~mlehmann/AnyEvent-7.12/lib/AnyEvent.pm#ENVIRONMENT_VARIABLES


    The way forward always starts with a minimal test.
      my $headers={} did the trick. Thanks ! Also thanks to the debug pointer.
Re: AnyEvent::HTTP not sending ContentLength ?
by NetWallah (Canon) on Mar 20, 2016 at 19:27 UTC
    %headerArray gets expanded out in this list context, resulting in:

    http_request "PUT" => $url, headers=> "Content-Length"=>2345, body=>$content, sub {do stuff here};
    which is bad syntax.

    Don't have time to research the right syntax now...

            This is not an optical illusion, it just looks like one.

Re: AnyEvent::HTTP not sending ContentLength ?
by bart (Canon) on Mar 21, 2016 at 08:13 UTC
    %headerArray, as you use it, is a flat list. What you want to pass is a hash reference, thus:
    http_request "PUT" => $url, headers=>\%headerArray, body=>$content, sub {do stuff here};
    Note the backslash before the percent sign.