I am getting an error trying to send a POST
request from a perl
script:
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->credentials($netloc,$realm,$username,$password);
use HTTP::Request::Common;
my $req = HTTP::Request::Common::POST($url,'content' => $conf);
$req->as_string()
is
POST http:.....
Content-Length: 31003
Content-Type: application/x-www-form-urlencoded
.....&createTime=1361370652541¶meters=HASH(0x28fd658)¶meters=HASH(0x28fd670)¶meters=HASH(0x28fd6e8)¶meters=HASH(0x28fd760)&nodes=32&.....&alerts=HASH(0x632d00)&alerts=HASH(0x245abd0)&.....
the error I get is
Unexpected character ('H' (code 72)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
which makes me suspect that the problem is with the repeated
parameters=HASH(...)
and alerts=HASH(...)
elements;
instead I want to see something like
alerts=%5B%7B%22email%22%3A%22foo%40bar.com%22%2C%22type%22%3A%221%22%2C%22timeout%22%3A%22%22%7D%5D
$conf
is a hash reference, and $conf->{"parameters"} and
$conf->{"alerts"}` are array references (whose elements are hash
references).
what am I doing wrong?
You can't post references; you presumably need to serialize them in some way; what is the server expecting?
From the error it looks like either the entire array or possibly each of the hashes in it should be serialized in JSON:
use JSON; # preferably have JSON::XS installed
my %prepared_conf = %$conf;
for my $field ( 'parameters', 'alerts' ) {
$prepared_conf{$field} =
JSON::to_json( $prepared_conf->{$field}, { 'ascii' => 1 } );
}
my $req = HTTP::Request::Common::POST($url,'content' => \%prepared_conf);