有谁知道为什么以下代码不会从请求主体中的WWW :: Curl :: Form对象发送POST数据?
#!/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');
$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;
这是我看到的POST请求(在详细输出和Wireshark中):
POST /post_test.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 0
正如您所看到的,没有Content-Type,Content-Length为0且正文中没有数据。
这是在Debian上使用libcurl3 7.21.0-2和libwww-curl-perl 4.12-1。
答案 0 :(得分:1)
尝试使用另一个包装器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;