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

hi, I'm working on a CGI page which constructs XML data, puts it into a scalar variable and posts it to a different website , where an ASP page receives the data and sends back an acknowledgement. The website which I'm posting to, requires that I pass a username and password in the URL (without which it generates an error "Username not sent in the posted data") Here's the block of the affected code

my $ua = LWP::UserAgent->new; my $remote_server = "100.100.100.100/ReceivingPage.asp?username= +test&password=pwd"; my $req = HTTP::Request->new(GET=>"http://$remote_server"); my $res = $ua->request($req); if ($res->is_success) { $req = HTTP::Request->new(POST=>"http://$remote_server"); + + $req->content_type('application/x-www-form-urlencoded'); my $postvars = $request_xml; $req->content_length(length($postvars)); $req->content($postvars); $res = $ua->request($req); if ($res->is_success) { my $xml_raw = $res->content; my $parsed = eval {XMLin($xml_raw) }; ...


Now my question is that when I'm POSTing the data, will the QueryString variables (username and password) be passed too ? Presently, the web site to which I'm posting the data to keeps complaining that I'm not sending the required Username and password.

Any help would be greatly appreciated...

Edit ar0n -- added code tags

Replies are listed 'Best First'.
Re: POSTing QueryString data using LWP::UserAgent
by Hero Zzyzzx (Curate) on Jan 08, 2002 at 06:34 UTC

    By embedding the username and password in the URL you're using a method of passing query info that is typical of the GET method. I'm not shocked at all that ASP doesn't like you mixing the GET and POST method in a single request. That's why it's complaining- your authentication information is being sent using the GET method, embedded in the query string.

    Does the site use cookies to track user authentication? If so, you need to set up a cookie jar using HTTP::Cookies to save the cookie, which will then be passed in all subsequent requests of the same useragent object.

    Ugh. That might be confusing. Just do a google search for "lwpcook" (or "man lwpcook") to find the excellent LWP cookbook. It will give you a much more elegant description of how to do this.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      Unfortunately,the website to which I'm posting to does not
      use cookies. I looked up the lwp cookbook, but it does not contain any example of POSTing form data and querystring data at the same time. Is there any workaround to this ?
Re: POSTing QueryString data using LWP::UserAgent
by gav^ (Curate) on Jan 08, 2002 at 09:14 UTC
    Does the ASP page expect the parameters passed to it by POST (rather than GET as you are trying)?

    Try (based on the HTTP::Request::Common docs):

    use HTTP::Request::Common; my $ua = LWP::UserAgent->new; my $res = $ua->request(POST 'http://100.100.100.100/ReceivingPage.asp' +, [username => 'test', password => 'test']);
      It should not work since screamingeagle already uses request content to pass XML document. Thus parameters can't be passed as POST - there is no space for them in HTTP request.

      This raises the question if screamingeagle is correct in his expectation that XML document should be passed as raw content of HTTP request. Maybe it should passed as POST parameter? Otherise if ASP page doesn't want username and password as GET parameters and as cookies then there is just no way to pass them.

      --
      Ilya Martynov (http://martynov.org/)

      Hi,
      I did follow your advice , with a little modification, and it worked , as far as passing the username and password; now the problem is that the XML data is not being passed .

      Here's what i did. :
      use HTTP::Request::Common; my $filetoopen = 'tp1.xml'; #XML file name my $ua = new LWP::UserAgent; my $res = $ua->request(GET 'http://2.2.2.2/receive.asp'); if ($res->is_success) { { no strict 'subs'; $res = $ua->request(POST 'http://2.2.2.2/receive.asp', Content +_Type => 'form-data', Content => [ username => test, password => test, init => [$filetoopen,$filetoopen], ]); } ...rest of the code...
      I keep getting an error message saying that the file is empty or invalid (I've confirmed that the file is not empty and it is in the same folder as the Perl file which is executing ) Is there something missing in the POST call ? my second question is whether instead of POSTing the XML data in a file, is it possible to post the XML data contained in a scalar variable ?
        Hi,
        I finally found the solution to my problem.Just thought that I'd share it with others...Here's how u post multipart/form-data using HTTP::Request::Common, without having to create a file containing the data (submitting the content directly ) :

        use HTTP::Request::Common; my $ua = new LWP::UserAgent; my $res = $ua->request(GET 'http://2.2.2.2/page.asp'); if ($res->is_success) { { no strict 'subs'; $res = $ua->request(POST 'http://2.2.2.2/page.asp', Content_Type => 'form-data', Content => [ username => test, password => test, init => [undef, "filename", content_type => 'text/xml', content => $request_xml,], ] ); } ...
        Hope this helps someone... :)