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

Trying to send a xml request to server, but it seems that the $xml_req is ignored and I do not get a reply

Code follows:

use LWP::UserAgent; use HTTP::Request::Common; # URL we're posting to my $URL = "http://whatever.com"; # The xml_request my $xml_req = <xml string></xml string>"; my $ua = LWP::UserAgent->new(); my $result = $ua->request(POST $URL, $xml_req); print "Content-type: text/plain\n\n"; print $result->as_string(); print $result->content();
=============================== I can connect to server, but the xml request is ignored...

Replies are listed 'Best First'.
Re: xml request
by fruiture (Curate) on Aug 04, 2002 at 16:29 UTC

    First fo all: there's a syntax error in your code. Please enable strict and warnings and please copy'n'paste your tested code, don't rewrite it.

    Secondly: What does "the xml request is ignored" mean? What happens instead? What error messages do you get?

    Have you read the HTTP::Request::Common manpage carefully? Your $xml_req is in no context there. What shall it be? A submitted field or something to be send in a header?

    #did you mean: POST $URL, [ xml => $xml_req ] #?

    try building a request of your needs using HTTP::Request's as_string() method. HTH

    --
    http://fruiture.de
      sorry about any mess, so far I see no syntax error,but I poste the code again. Thanks for your help by the way!! What happens is when I send the request that it seems that the xml string is not being passed on and the response I get is as if I sent the request without the string
      #!/usr/bin/perl use LWP::UserAgent; use HTTP::Request::Common; my $URL = "url.com"; # The xml_request my $xml_req = " <Row> <Address>H</Address> <Name>MUSTER/MAX</Name> <Age>34</Age> </Row>"; my $ua = LWP::UserAgent->new(); my $result = $ua->request(POST $URL,["form_param" => $xml_req]); print "Content-type: text/plain\n\n"; print $result->as_string(); print $result->content();

        Again: enable strict and warnings, especially when you're looking for a bug!

        The code should work now. But let's do some debugging:

        #$URL and $xml_req initialised print "Content-type: text/plain\r\n\r\n"; #rather use CGI.pm anyway print "URL: $URL\nXML: $xml_req\n-----\n"; my $ua = LWP::UserAgent->new(); print "Useragent: $ua\n"; #will print reference representation my $req = POST $URL,['form_param'=>$xml_req]; print "Request:\n",$req->as_string(),"\n-----\n"; my $result = $ua->request($req); # rest is clear...

        The problem is the generated Request (speculation). You can be sure after reading the documentation of HTTP::Request::Common

        --
        http://fruiture.de
Re: xml request
by rjray (Chaplain) on Aug 04, 2002 at 23:15 UTC

    The POST short-cut provided by HTTP::Request::Common expects to encode a list of parameters as a content-type of application/x-www-form-urlencoded. This isn't what you want to have done, so you can't use HTTP::Request::Common in this case.

    Look at HTTP::Request instead, and create a request as such:

    my $request = HTTP::Request->new(POST => $url); $request->content($xml);

    Then you can pass $request as the parameter to the $ua->request() method.

    --rjray

      Dear monk, this is all really weired, here part of the code and then the response CODE:

      $ua = LWP::UserAgent->new; my $request = HTTP::Request->new(POST => $url); $request->content($xml_req); $response = $ua->request($request);
      RESPONSE from Server:

      HTTP/1.1 500 Internal Server Error Connection: close Date: Mon, 05 Aug 2002 09:40:10 GMT Server: Apache/1.3.22 (Unix) mod_jk mod_ssl/2.8.5 OpenSSL/0.9.6c Content-Type: text/html; charset=iso-8859-1 Client-Date: Mon, 05 Aug 2002 09:40:10 GMT Client-Peer: ip number Title: 500 Internal Server Error <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>500 Internal Server Error</TITLE> </HEAD><BODY>

      Internal Server Error

      The server encountered an internal error or misconfiguration and was unable to complete your request.

      Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error.

      More information about this error may be available in the server error log.

      </BODY></HTML>

        This means that the server you contacted had a critical failure at some juncture. Unfortunately, without knowing anything about the server, we'll be hard-pressed to offer any further advice.

        --rjray