开发者

WWW::Curl fails to attach WWW::Curl::Form data in POST body

Does anyone know why the following code wouldn't send the POST data from the WWW::Curl::Form object in the request's body?

    #!/usr/bin/perl
    use strict;
    use warnings;
    use WWW::Curl::Easy;
    use WWW::Curl::Form;

    my $curl = new WWW::Curl::Easy();
    $curl->setopt(CURLOPT_VERBOSE, 1);
    $curl->setopt(CURLOPT_NOSIGNAL, 1);
    $curl->setopt(CURLOPT_HEADER, 1);
    $curl->setopt(CURLOPT_TIMEOUT, 10);
    $curl->setopt(CURLOPT_URL, 'http://localhost/post_test.php');

    my $curlf = new WWW::Curl::Form();
    $curlf->formadd('a','b');
    开发者_如何学Go$curlf->formadd('c','d');
    $curlf->formadd('e','f');
    $curlf->formadd('g','h');
    $curlf->formadd('i','j');
    $curl->setopt(CURLOPT_HTTPPOST, $curlf);

    my $resp = '';
    open(my $resp_fh, ">", \$resp);
    $curl->setopt(CURLOPT_WRITEDATA, $resp_fh);

    my $retcode = $curl->perform();
    die($retcode) if ($retcode != 0);

    print $resp;

This is the POST request I see (both in the verbose output and through Wireshark):

    POST /post_test.php HTTP/1.1
    Host: localhost
    Accept: */*
    Content-Length: 0

As you can see there's no Content-Type, the Content-Length is 0 and there's no data in the body.

This is on Debian using libcurl3 7.21.0-2 and libwww-curl-perl 4.12-1.


Try to use another wrapper, Net::Curl:

#!/usr/bin/perl
use strict;
use warnings;
use Net::Curl::Easy qw(:constants);
use Net::Curl::Form qw(:constants);

my $curl = new Net::Curl::Easy();
$curl->setopt(CURLOPT_VERBOSE, 1);
$curl->setopt(CURLOPT_NOSIGNAL, 1);
$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_TIMEOUT, 10);
$curl->setopt(CURLOPT_URL, 'http://localhost/post_test.php');

my $curlf = new Net::Curl::Form();
$curlf->add(CURLFORM_COPYNAME ,=> 'a', CURLFORM_COPYCONTENTS ,=> 'b');
$curlf->add(CURLFORM_COPYNAME ,=> 'c', CURLFORM_COPYCONTENTS ,=> 'd');
$curlf->add(CURLFORM_COPYNAME ,=> 'e', CURLFORM_COPYCONTENTS ,=> 'f');
$curlf->add(CURLFORM_COPYNAME ,=> 'g', CURLFORM_COPYCONTENTS ,=> 'h');
$curlf->add(CURLFORM_COPYNAME ,=> 'i', CURLFORM_COPYCONTENTS ,=> 'j');
$curl->setopt(CURLOPT_HTTPPOST, $curlf);

my $resp = '';
open(my $resp_fh, ">", \$resp);
$curl->setopt(CURLOPT_WRITEDATA, $resp_fh);

my $retcode = $curl->perform();
die($retcode) if ($retcode != 0);

print $resp;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜