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

I am trying to post some xml to a web service but when I do this my browser returns and error about the format of my xml. However, if I try this from a command line I get what I expect in return. I think the issue might be related to the format of the data I am trying to post. I thought I might resolve this issue by trying to encode the message using uri_escape from the URI::Escape module but that did not appear to resolve the issue. Here is a sample of my code so far.

#!/usr/bin/perl use warnings; use strict; use XML::Smart; use LWP::UserAgent; my $doc = XML::Smart->new(); $doc->{PHC_LOGIN}{USERID} = 'john.doe'; $doc->{PHC_LOGIN}{USERID}->set_node(1); $doc->{PHC_LOGIN}{USERPASSWORD} = 'FAKEPASSWORD'; $doc->{PHC_LOGIN}{USERPASSWORD}->set_node(1); $doc->{PHC_LOGIN}{PARTNERID} = '111'; $doc->{PHC_LOGIN}{PARTNERID}->set_node(1); $doc->save('output.xml'); my $sendXML="output.xml"; my $webpage="https://example.com/Login/AutoLogin.aspx"; my $message=""; open (XML,$sendXML); while (<XML>) { $message .="$_"; } close XML; my $ua = LWP::UserAgent->new; $ua->add_handler("request_send", sub { shift->dump; return }); $ua->add_handler("response_done", sub { shift->dump; return }); $ua->proxy('https', 'http://proxy.example.net:8080/'); my $header= $ua->default_headers; my $response = $ua->post($webpage, Content_Type => 'text/xml', Content + => $message); $response->decoded_content; if ($response->is_success) { my $html = $response->content; if ( $html =~ /\<REDIRECTURL\>(.*)\<\/REDIRECTURL\>/) { my $dest = $1; $ua->post($dest); } else { print "No redirect url found\n"; }; } else { die print $response->status_line; print $response->decoded_content; };

Replies are listed 'Best First'.
Re: http url encoding issues.
by argos (Initiate) on Aug 24, 2014 at 18:57 UTC

    I changed the Content_Type to "text/html" and it appears get a little further in firefox but not in IE. I still see an error in my browser "Invalid at the top level of the document" In firefox it prints the three values to the browser which is not what I wanted but no format error.

      well, I noticed that if I use fidder (an IE proxy) it tells me that their is a content length is mismatch. I wonder if I should be setting the content length? In Firefox I don't see that issue, the code just returns the xml to the screen but does redirect as I would expect.

Re: http url encoding issues.
by Anonymous Monk on Aug 24, 2014 at 22:19 UTC
    What error?

      The error in IE tells "me that the Invalid at the top level of the document. Error processing resource 'http://example.com/cgi-bin/xml-demo.pl'. ... </PHC_LOGIN>\n" ------------^ In Firefox I just get back the output from the xml file created in the script but I am really expecting to be redirected as happens when I run the script from a command line.

        Hmm, how are you executing this program from the firefox?

        FWIW, this program doesn't appear to speak HTTP on STDOUT