bob_saginowski bob_saginowski - 4 months ago 19
Perl Question

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

I am using

HTTP::Tiny
to do a sample http client (only get and post requests).
GET
requests are working fine but I have an issue when I try to do a
POST
request. Here is the code:

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};
}


Where
%params
hash is like
{ key1 => "val1", key2 => "val2" }
. The message I get is
Attempt to use reference as lvalue in substr at /usr/lib/perl5/vendor_perl/5.22/HTTP/Tiny.pm line 806, <STDIN> line 7.
and I have not idea how to resolve it.

Answer

It looks like you actually want:

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