hallikpapa has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks. I am definetly making this harder than it should be. For example, I open a file with XML already properly formatted and read it into @data. Then I simply want to push all that XML as one big stream to a specific URL. I can't get it to work. I am not sold on using LWP::UserAgent, or anything, but whatever will work. :) Here is what I am trying to do:
my $browser = LWP::UserAgent->new; my $url = 'http://localhost:8180/request'; open(FILE,"output.xml"); my @data = <FILE>; my $response = $browser->post( $url, Content => @data ); #print @data;
When I print @data, it shows the full XML. Doing a post like this ONLY sends the very first XML tag, not all of the contents in @data. However, if I do a post like this:
my $response = $browser->post( $url, Content => \@data);
It sends the entire contents, but ruins all of the tags.
%3CcontactTn%3E%3C%2FcontactTn%3E%0A&++++++++++++++%3CcontactEmail%3E% +3C%2FcontactEmail%3E%0A=++++++++++++++%3Caddress%3E%0A&++++++++++++++ +++%3CbuildingNo%3E%3C%2F
Please help! Thanks.

Replies are listed 'Best First'.
Re: Sending XML data is simple right?
by erroneousBollock (Curate) on Nov 15, 2007 at 17:45 UTC
    Try this:

    my $response = $browser->post($url, Content => join('', @data));

    ... and then read up on the fat comma (=>) operator in perlop.

    What you're doing is equivalent to:

    my $response = $browser->post($url, Content => $data[0], $data[1] => $data[2], $data[3] => $data[4], ... );

    ... which is not expected by the post method.

    Update: Oops, thanks Jenda :-/

    -David

      This would cause the sent data to look like this

      <?xml version="1.0"?> <root> <othertag>foo</othertag> </root>
      The elements of the @data array do contain the newlines already.

      If you want to merge the array into a single string you should use join( '', @data)

Re: Sending XML data is simple right?
by Jenda (Abbot) on Nov 15, 2007 at 18:01 UTC

    You need to read the data into a single scalar, not into an array:

    ... my $data = do {local $/; <FILE>;}; my $response = $browser->post( $url, Content => $data ); ...
    As you want to send it all all to somewhere it's pointless to ask Perl to split the data into individual lines and then try to merge it all back together.

Re: Sending XML data is simple right?
by samtregar (Abbot) on Nov 15, 2007 at 18:01 UTC
    The previous comment addressed why your first attempt didn't work. The second one is more subtle. LWP::UserAgent isn't ruining your tags - it's encoding them for transmission to the server on the other end of the connection. You can change the kind of encoding it does, but you shouldn't need to worry about it - the server should decode that back into the original XML.

    -sam

Re: Sending XML data is simple right?
by hallikpapa (Scribe) on Nov 15, 2007 at 18:29 UTC
    It is indeed easy. I have a better understanding of things, and never read the perlops page. I think I will be doing that tonight. Thanks guys for all your input, I spread all my votes around in this thread. :)