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

So, I want to emulate the following curl command:

curl -X POST -d @test.xml http://user:password@server.com:5555/ --head +er "Content-Type:text/xml"

Using LWP::Curl I've tried this:

my $hash_form = { 'data' => '@test.xml', 'header' => 'Content-Type:text/xml', }; my $content = $lwpcurl->post($post_url, $hash_form, $referer);

But I don't get the content of the file passed. Any suggestions would be appreciated.

Replies are listed 'Best First'.
Re: curl http post using LWP::Curl
by NetWallah (Canon) on Jun 07, 2016 at 17:02 UTC
    Untested : You may need something clunky like:
    use Net::Curl::Easy qw(:constants); $lwpcurl->{agent}->setopt( CURLOPT_HEADERDATA, ['Content-Type:text/xml +'] ); # -> attempt your POST here.

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

      Attempted:

      $lwpcurl->{agent}->setopt( CURLOPT_HEADERDATA, ['Content-Type:text/xml +'] );

      But get:

      libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5 Can't use an undefined value as filehandle reference at /home/mware/httppost/lib/perl5/site_perl/5.8.8/LWP/Curl.pm lin +e 236 (#1) (F) A value used as either a hard reference or a symbolic referenc +e must be a defined value. This helps to delurk some insidious errors. Uncaught exception from user code: Can't use an undefined value as filehandle reference at /home/ +mware/httppost/lib/perl5/site_perl/5.8.8/LWP/Curl.pm line 236. at /home/mware/httppost/lib/perl5/site_perl/5.8.8/LWP/Curl.pm line 23 +6

      This error goes away if I remove that line of code.

        This error happens in the ->post method of LWP::Curl, a place that doesn't immediately relate to the line you posted above.

        Can you please post a short, self-contained example program that reproduces the problem? Maybe part of the problem is Perl 5.8's spotty support for in-memory filehandles or something else, but without having code to easily reproduce your situation, that's hard to say.

Re: curl http post using LWP::Curl
by Corion (Patriarch) on Jun 07, 2016 at 16:25 UTC

    If the -d @test.xml command is supposed to read the file test.xml into memory and send it as the body of the POST request, I guess you'll have to do the part of reading the file in Perl.

    my $filename = 'test.xml'; open my $fh, '<', $filename or die "Couldn't read '$filename': $!"; binmode $fh, ':raw'; my $content = do { local $/; <$fh> }; # sluuurp $hash_form = { ... data => $content ... }

      I thought about that approach. As a short cut I did this:

      my $content = "<test>test</test>"; $hash_form = { data => $content ...

      Still does not push the content to the host properly.

        Have you tried debugging it? What does get sent?

        When dealing with HTTP (well, any kind of TCP) problems, usually Wireshark or tcpdump can tell you what exactly goes over the wire. Usually that's a first hint as to what goes wrong where.

        Note that your seeming addition of headers is not supported by the documentation in Net::Curl.