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

Is there a Perl subroutine that converts a string into a MIME format called "x-www-form-urlencoded". I want the Perl version of java.net.URLEncoder.encode.

http://java.sun.com/j2se/1.3/docs/api/java/net/URLEncoder.html

I want to create parameters for a POST request.

CGI::escape doesn't seem to do the job. I need each space character converted into a plus sign, not %20.

I am trying to avoid reinventing the wheel.

Replies are listed 'Best First'.
Re: MIME format x-www-form-urlencoded
by Joost (Canon) on May 19, 2005 at 15:15 UTC
Re: MIME format x-www-form-urlencoded
by ikegami (Patriarch) on May 19, 2005 at 15:24 UTC
    Check out HTTP::Request::Common
    use HTTP::Request::Common qw( POST ); my $request = POST 'http://www.server.com/script.cgi', [ userid => 'ikegami', answer => 'yes indeed', ]; print($request->content, "\n"); # Prints: # userid=ikegami&answer=yes+indeed

    It even uses "+" even though "%20" is equivalent. If your CGI script doesn't handle "+" and "%20", it's broken since you can't tell the browser which way to encode the space.