http://www.perlmonks.org?node_id=205247


in reply to Can't Post XML to SSL Site

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 ]);

Replies are listed 'Best First'.
Re: Re: Can't Post XML to SSL Site
by dpatrick (Scribe) on Oct 15, 2002 at 04:49 UTC
    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( 'dev.xmlsite.com', 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.

    dpatrick
    - I think scsh is cool.
    Open Sourceror, Perlmonk
    http://perlmonk.org/~dpatrick

      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:

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

      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
        Okay, forget what I said about encoding. Here's what I know, but first I'll rephrase my question:

        "Help. I need to post XML data (that is indeed XML as I'm using XML::AutoWriter to parse the vendor supplied DTD and generate valid XML data from it, which is then validated against the DTD before it even leaves XML::AutoWriter by XML::ValidWriter) to an SSL site for processing. There is no path info, just a hostname and port. The vendor's XML API documentation (which doesn't actually provide an API, just a definition of the XML elements which can be accepted) makes no mention of what content type to use for posting or anything. All they said is that they want the XML data (xml declaration or not, which I found out from testing) POSTED in a specific form variable, XMLSITE_XML for instance. Here is my code..."

        What I found out after bouncing tests off of the server:

        1. The server (application) doesn't read the content type. I can send text/xml, text/plain, text/arse, or not even specify content type.

        2. Whatever encoding is applied by Net::SSLeay (which I think is none) is what the server wants

        3. I really don't think there is any encoding done. Here's why: the "API" document had example code in Java. The Java submission was totally no frills, open a raw SSL socket, prepend "XMLSITE_XML=" to the generated XML data stored in a scalar, print the scalar to the SSL socket handle, receive the response back into a scalar, then close the handle.

        Thus this is what I'm doing (I can also post the XML::AutoWriter stuff from a different module if anyone is interested):
        sub postXML { my $self = shift; my $finalXML = $self->getFinalXML(); my $POST_URL = $self->getProcessingURL(); use URI::URL; my $url = URI->new($POST_URL); my $domain = $url->host(); my $port = $url->port(); use Net::SSLeay; my ($page, $response, %reply_headers) = Net::SSLeay::post_https( $domain, $port, '', Net::SSLeay::make_headers( '', # notice 'Content-type' omitted 'User-Agent' => 'ClearCommerce::XML::POST/0.1' ), "CLRCMRC_XML=$finalXML" ); return $page; }
        And this works wonderfully so far.

        For those interested, these are the reply headers from the server, which prompted me to (incorrectly) think I should send Content-type text/xml:
        CONTENT-TYPE: text/xml CONTENT-LENGTH: 5152 CONNECTION: close SERVER: CCE_XMLIC/1.0 Response: HTTP/1.1 200 OK Page: <?xml version="1.0" encoding="UTF-8">? ... XML response follows


        dpatrick
        - I think scsh is cool.
        Open Sourceror, Perlmonk
        http://perlmonk.org/~dpatrick