I am using LWP::UserAgent to send a POSt request to a remote server.
my $type = 'post'; my $url = redacted; my $headers = { 'Content' => { 'field1' => '1', 'field2' => [{'subfield1' => 'x', 'subfield2' => 'y'}, possibly mo +re in array], 'field3' => {subfield3 => int, 'subfield4' => 'string'}, }, } my $response = $ua->$type( $url, %{$headers} );
For testing I also added in
$headers->{Content}->{'foo'} = ['bar', 'baz']
I get an error back and it looks like my hash reference and array ref for field2 and field3 and passed as strings not the underlying objects so the _content string looks like The foo part parses correctly which tells me that LWP or HTTP::Request::Common POST doesn't like converting the hashes.
"field3": "HASH(0x2308c10)"
Obviously this isn't what I want. Now previously I had solved this by doing horrible things with strings.
$args = {data => $args}; my @newstring; foreach my $key (keys %{$args}) { foreach my $value (keys %{$args->{$key}}) { push @newstring, $key.'['.$value.']='.$args->{$key}->{$value}; } } $args = join('&',@newstring); my $response = $ua->post( $url, Content => $args );
That was in a different revision of the remote server when it wanted things in a data structure variable. Essentially I was crafting my own string _content string and passing it in. The server understood it but it was a ugly way of doing it. I'm looking for a solution that doesn't involve me having to construct the string myself

In reply to LWP::UserAgent POST pass hash by Stoney2005

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.