Perl http post 请求:尝试将引用用作 substr 中的左值

Perl http post request: Attempt to use reference as lvalue in substr

我正在使用 HTTP::Tiny 做一个示例 http 客户端(仅获取和 post 请求)。 GET 请求工作正常,但我在尝试执行 POST 请求时遇到问题。这是代码:

sub postRequest {
    my %params = @_;
    my $url= "http://localhost:3001/Perform";
    my %opt;
    $opt{content} = \%params;
    my $http = HTTP::Tiny->new();
    my $response = $http->request("POST", $url, \%opt);

    # my $response = $http->post($url, {content => \%params}); # not working too

    unless ($response->{success}) {
        die "Unsuccessful request to " . $url. "\n";
    }

    print "response: " . $response->{content} . "\n";
    return $response->{content};
}

其中 %params 散列类似于 { key1 => "val1", key2 => "val2" }。我收到的消息是 Attempt to use reference as lvalue in substr at /usr/lib/perl5/vendor_perl/5.22/HTTP/Tiny.pm line 806, <STDIN> line 7.,我不知道如何解决它。

HTTP::Tiny::request 的文档允许哈希引用 \%options

$response = $http->request($method, $url, \%options);

对于它的键来说如下

Valid options are:

  • headers [ ... ]

  • content — A scalar to include as the body of the request OR a code reference that will be called iteratively to produce the body of the request

这意味着 content 键的值应该是标量(字符串)或代码引用。代码中 content 的值是哈希引用 \%params.

将其更改为字符串或代码引用。

看来你真的想要:

$http->post_form($url, \%params);