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

Hi,
I am using XML Post to a url, in HTTP::Request::Common can i escape some additional characters, and also i need to unescape (i mean remove the default unescaping of the characters such as colon ':'). is this possible?
Further can i get a list of charcters that HTTP::Request::Common escapes by defualt?
Pl. reply.
In need of urgent perl wisdom!
Thanks
abhishek
  • Comment on HTTP::Request::Common - Unescape some characters

Replies are listed 'Best First'.
Re: HTTP::Request::Common - Unescape some characters
by Corion (Patriarch) on Jun 24, 2009 at 09:11 UTC
      I have checked URI::Escape , but i believe the HTTP::Request::Common is an easy way as per its documentation as it escapes automatically.
      Help Sought
      Thansk
      abhi

        Please show us data in the form you have it, and in the form you want it, and how it is used. That will help us help you better, because currently, it is quite unclear to me what data you have, where you use it and where you think that HTTP::Request::Common will escape or unescape it.

Re: HTTP::Request::Common - Unescape some characters
by ikegami (Patriarch) on Jun 24, 2009 at 14:00 UTC

    ":" in a value must be escaped. You're asking us to help you generate invalid urls. What's the real problem?

    Update: Or maybe not. It's conditional:

    If data for a URI component would conflict with a reserved character's purpose as a delimiter, then the conflicting data must be percent-encoded before the URI is formed.

    It's still valid to escape it, though, and most clients will. You can't even trust proxies to preserve it.

      Hi,
      Ok let me give you an example for the string:
      The original string is:
      http://api.url?data=<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE MESSAGE SYSTEM "http:// +127.0.0.1/psms/dtd/message.dtd" ><MESSAGE><USER USERNAME="test" PASSW +ORD="XXXXXX"/><SMS UDH="0" CODING="1" TEXT="The flight # <101> "DEL" +to "BLR" is delayed and it's revised time will be informed later. Hav +e a nice day!" PROPERTY="0" ID="1"><ADDRESS FROM="ValueFirst" TO="91X +XXXXXXXXX" SEQ="1" TAG="some clientside random data" /></SMS></MESSAG +E>&action=send

      The escaped string is:
      http://api.url/psms/servlet/psms.Eservice2?data=%3C?xml%20version=%221 +.0%22%20encoding=%22ISO-8859-1%22?%3E%3C!DOCTYPE%20MESSAGE%20SYSTEM%2 +0%22http://127.0.0.1/psms/dtd/message.dtd%22%20%3E%3CMESSAGE%3E%3CUSE +R%20USERNAME=%22test%22%20PASSWORD=%22valueone%22/%3E%3CSMS%20%20UDH= +%220%22%20CODING=%221%22%20TEXT=%22The%20flight%20No.%20SG101:%20DEL% +20to%20BLR%20is%20delayed%20and%20it's%20revised%20time%20will%20be%2 +0informed%20later.%20Have%20a%20nice%20day!%22%20PROPERTY=%220%22%20I +D=%221%22%3E%3CADDRESS%20FROM=%22ValueFirst%22%20TO=%22919891153528%2 +2%20SEQ=%221%22%20TAG=%22some%20clientside%20random%20data%22%20/%3E% +3C/SMS%3E%3C/MESSAGE%3E&action=send

      Pl. let me know how to generate the escaped string:
      Please help me as this is getting crucial for me
      Thanks
      abhi

        That's very long for a URL. Most serves have a limit on the length of the URLs they can handle.

        Also, your XML is invalid. You have unescaped double quotes in a double quoted attributes value, and "<" is not allowed to appear in attribute values. You need to use "&quot;" and "&lt;". Parsers might excuse the latter, but the former will definitely result in an error.

        TEXT="The flight # <101> "DEL" to "BLR" is ..."
        should be
        TEXT="The flight # &lt;101> &quot;DEL&quot; to &quot;BLR&quot; is ..."

        But to answer your question, you can use URI to build a URL (a kind of URI).

        use URI qw( ); chomp( my $data = <<'__EOI__' ); <?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE MESSAGE SYSTEM "h +ttp://127.0.0.1/psms/dtd/message.dtd" ><MESSAGE><USER USERNAME="test" + PASSWORD="XXXXXX"/><SMS UDH="0" CODING="1" TEXT="The flight # <101> +"DEL" to "BLR" is delayed and it's revised time will be informed late +r. Have a nice day!" PROPERTY="0" ID="1"><ADDRESS FROM="ValueFirst" T +O="91XXXXXXXXXX" SEQ="1" TAG="some clientside random data" /></SMS></ +MESSAGE> __EOI__ my $url = URI->new('http://api.url/psms/servlet/psms.Eservice2'); $url->query_form( data => $data, action => 'send', ); print "$url\n";
        #!/usr/bin/perl -- use strict; use warnings; use HTTP::Request::Common; use URI; { my $uri = URI->new('http://localhost/'); print POST( $uri, ['data' => '<data>'])->as_string; $uri->query_form('data' => '<data>'); print GET( $uri )->as_string; } __END__ POST http://localhost/ Content-Length: 15 Content-Type: application/x-www-form-urlencoded data=%3Cdata%3E GET http://localhost/?data=%3Cdata%3E