Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Can't Post XML to SSL Site

by dpatrick (Scribe)
on Oct 14, 2002 at 17:20 UTC ( [id://205156]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow monks, I'm at a clients site so my ass is grass right this very moment if I can't get this going rather quickly. I've to the following code trying to post an XML document to an SSL site for parsing:
sub postXML { my $self = shift; my $finalXML = $self->getFinalXML(); my $POST_URL = $self->getProcessingURL(); # print $finalXML, "\n"; # use Net::SSLeay; # my ($page, $response, %reply_headers) = # Net::SSLeay::post_https( # '', # 11800, # '', # Net::SSLeay::make_headers('Content-type' => 'application/ +x-www-form-urlencoded'), # Net::SSLeay::make_form(CLRCMRC_XML => "$finalXML") # ); # print 'Response: ', $response, "\n"; # print 'Page: ', $page, "\n"; use LWP::UserAgent; my $userAgentObject = LWP::UserAgent->new(); $userAgentObject->agent("ClearCommerce::XML::POST/0.1"); use HTTP::Request; my $postRequestObject = HTTP::Request->new(POST => $POST_URL); $postRequestObject->content_type('application/x-multipart-form-dat +a'); $postRequestObject->content("CLRCMRC_XML=$finalXML"); print 'Posting to ', $self->{processingURL}, " the following:\n"; print $finalXML, "\n"; my $postRequestResponseObject = $userAgentObject->request($postReq +uestObject); print $userAgentObject->request($postRequestObject)->content(); if ($postRequestResponseObject->is_success) { print $postRequestResponseObject->content(), "\n"; } else { print "Bad Request.\n"; } }
No running the commented out code (which actually works; the LWP code doesn't do anything but return my Bad Request print) gives me back a response form the server saying that the XML was not included or was formattted badly. Now I know that it's formatted correctly from printing the $finalXML variable. Do I need to escape the XML somehow or something? I'm totally at a loss for what to do at this point. I was up all night trying to get this working. Any help that anyone could contribute would be wonderful and greatly appreciated. D.

- I think scsh is cool.
Open Sourceror, Perlmonk

Replies are listed 'Best First'.
Re: Can't Post XML to SSL Site
by Anonymous Monk on Oct 14, 2002 at 20:54 UTC

    This is a guess but I've added an explicit HTTP::Headers object, set the content-type and included your XML content directly in the HTTP::Request constructor. I used something I previously worked on as a reference. I hope this helps.

    require HTTP::Headers; my $header = HTTP::Headers->new(); $header->header( 'Content-Type' => 'text/xml' ); require HTTP::Request; my $postRequestObject = HTTP::Request->new( POST => $POST_URL, $header, [ CLRCMRC_XML => $finalXML ] );
Re: Can't Post XML to SSL Site
by iburrell (Chaplain) on Oct 14, 2002 at 22:52 UTC
    How do you want to POST the XML? Do you want to do a normal form POST or use multipart/form-data? The "key=value" format is used by the standard urlencoded format. In that case, you would need to encode the value. Be aware that most web servers aren't going to like receiving huge values with that kind of POST. Luckily, HTTP::Request will construct form-data requests for you if you give it the right arguments.
    $postRequest = HTTP::Request('POST' => $POST_URL, Content_Type => 'form-data', Content => [ CLRCMRC_XML => $finalXML ]);
      Okay, thank you both! However, neither suggestion did what I need it to do. I've figured out that the server is not going to unencode anything so the XML needs to be sent in plain text, no encoding. This works:
      my $finalXML = $self->getFinalXML(); my $POST_URL = $self->getProcessingURL(); print $finalXML, "\n"; use Net::SSLeay; my ($page, $response, %reply_headers) = Net::SSLeay::post_https( '', 11800, '', Net::SSLeay::make_headers('Content-Type' => 'text/xml'), "CLRCMRC_XML=$finalXML" ); foreach my $key (keys %reply_headers) { print "$key: ", $reply_headers{$key}, "\n"; } print 'Response: ', $response, "\n"; print 'Page: ', $page, "\n";
      ...and it's nice because I've got my XML response in a separate variable out of gate: $page. I'd like to do the above using LWP, but I can't figure out how to send content unencoded, ie. plain text. I'm stumped.

      - I think scsh is cool.
      Open Sourceror, Perlmonk

        That's incomprehensible. XML is always encoded whether using Latin1, UTF-8, KOI-5 or whatever. Probably the most "plain text" version you'll see is Latin1. That doesn't mean the XML doesn't need to be encoded properly. XML is always encoded. Perhaps you meant that the server accepts XML-like data but that it's not really XML? That'd be the case if you mean to pass in things like this:

          <k> 10 < 20 </k>
        <?xml encoding="ISO-8859-1" version="1.0?>
          <k> 10 < 20 </k>

        In all encodings of XML I'm aware of you are required to rewrite "<" as something like "<" / "<" / "<". Or have you thrown me a loop here on the encoding business?

        __SIG__ printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B:: +svref_2object(sub{})->OUTSIDE
Re: Can't Post XML to SSL Site
by DapperDan (Pilgrim) on Oct 15, 2002 at 15:10 UTC

    What you are trying to do is less than totally clear from the question you asked, but I think the solution you found and posted subsequently cleared it up somewhat. From my point of view there are only two possibilities of what you are trying to do:

    1. POST some XML as a parameter of a form, e.g. some XML that was pasted or typed in a HTML <input type="text"> or <textarea> widget.
    2. POST some XML as the body of a HTTP request, with a Content-type header of text/xml (or whatever). This is a perfectly legitimate thing to want to do, particularly if you are building a RESTful web service.

    It seems pretty clear from your answer that what you're talking about is no. 2. Care to comment?

    While this is an aside (because you got the POST over HTTPS to work and that's probably all that matters to you), I feel compelled to point out that what you're posting isn't XML, it appears to be a string "CLRCMRC_XML=$finalXML", for whatever value of $finalXML. This makes the semantics of the HTTP request quite unclear; the Content-type header is blatantly misleading (or even just 'wrong').

    You mightn't give a rat's ass about this because the semantics are in your brain; nonetheless I think I would just give that a mime type of plain text or something in the Content-type header.

      Check my response to diotalevi above.

      It actually turns out that it is a combination of 1 and 2, as you'll see from the above response. They're doing things in a strange way (the folks who are accepting the XML), I'm guessing possibly so that they can accept other data in other form variables, but this other data could just as easily be added into one XML document...I don't know. Anyway, yes you are right, the Content-type was incorrect, but it was not being parsed on the accepting end anyway. You are also right again in that I'm not posting XML. I'm posting a form, without the proper content-type (which I could, in fact, send anyway if I wanted to because they're not parsing it anyway), with valid XML data in one of the form fields as a string.

      It's weird. But Perl Monks rock!

      - I think scsh is cool.
      Open Sourceror, Perlmonk

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://205156]
Approved by ybiC
Front-paged by diotalevi
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-06-22 06:01 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.