Re: POST data to a request stream

Gisle Aas <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.lwp
Message-ID <[email protected]>
On Wed, May 27, 2009 at 16:43, Terrence Brannon <[email protected]> wrote:
> I have modified the code I read in an earlier thread on writing data to a
> request stream:
> http://www.mail-archive.com/[email protected]/msg06373.html
>
>   my $ua = LWP::UserAgent->new();
>   my $req = HTTP::Request->new(POST => $self->url);
>   my $xml = $self->xml_output;
>   my $length = length($xml);
>
>   $req->content($xml);
>   $req->header('Content-length' => $length);
>   $req->content_type("application/octet-stream");
>
>   my $res =  $ua->request($req);
>   my $response_data = $res->content;
>
> But the error I am getting from them is 'Root node of XML is missing' -
> which basically means to me that they did not get anything written to the
> request stream.

It could also mean that your XML is in error.

> They provide an example of how the POST request should be done in .NET. So
> my goal is to use LWP to mimic the exact .NET commands below. The only
> difference that I can see is that I am not creating a "requestStream" object
> and then manually issuing requestStream.Write(postDataBytes, 0,
> postDataBytes.Length); but I assume that LWP is doing something similar
> under the hood.

AFAICT you should end up with the same kind of request on the wire.
The setting of Content-Length should not be needed, but it should not
be harmfull either.

--Gisle


> string UploadURL = "https://www.perlrocks.com/submit.aspx?nqid=partnerid"
>
> HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UploadURL);
>
> string postData = null;
> request.Method = "POST";
> request.KeepAlive = false;
> request.Timeout = 180000;
>
> postData = XMLString;
>
> request.ContentType = "application/octet-stream";
> byte[] postDataBytes = Encoding.UTF8.GetBytes(postData);
> request.ContentLength = postDataBytes.Length;
>
> Stream requestStream = request.GetRequestStream();
> requestStream.Write(postDataBytes, 0, postDataBytes.Length);
> requestStream.Close();
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.