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

Is there an "elegant" way to get the current URL (including the Query String)?

I've been using this:

$ENV{QUERY_STRING} =~ s/(['"])/uc sprintf("%%%02x",ord($1))/eg; my $url = join ( "?", CGI::url ( -absolute => 1 ) || "/", $ENV{QUERY_S +TRING} || () );

I would have liked to use CGI::url ( -absolute => 1, -query_string => 1 ) but this returns the POSTed params instead of the Query String.

Replies are listed 'Best First'.
Re: How to get current URL with Query String
by Your Mother (Archbishop) on May 10, 2013 at 16:51 UTC

    Another.

    use strictures; use CGI ":standard"; use URI; my $requested = URI->new( CGI::url() ); $requested->query( $ENV{QUERY_STRING} || $ENV{REDIRECT_QUERY_STRING} ) if url_param(); print header(), start_html(), h1("OHAIURI"), blockquote($requested), end_html();
        I wanted to quickly input here. I've been using this, especially where I have apps that run on both http:// and https:// In my case where I use OOP it's usually a subroutine in the main package, but this works just as well, calling the $page_url variable. Some smart concatenation, no additional modules, strictly native...
        my $page_url = 'http'; if ($ENV{HTTPS} = "on") { $page_url .= "s"; } $page_url .= "://"; if ($ENV{SERVER_PORT} != "80") { $page_url .= $ENV{SERVER_NAME}.":".$ENVSERVER_PORT}.$ENV{REQUEST_U +RI}; } else { $page_url .= $ENV{SERVER_NAME}.$ENV{REQUEST_URI}; }
        what do you think?
Re: How to get current URL with Query String
by põhjapõder (Novice) on May 10, 2013 at 12:58 UTC
    I would have liked to use CGI::url ( -absolute => 1, -query_string => 1 ) but this returns the POSTed params instead of the Query String.

    If I understood you correclty, you are processing a POST request with POST data and a query string and like to retrieve just the URL parameters. In that case the following should work for you:

    #!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new(); sub url_query_string { my ($q) = @_; # get the names of all URL parameters my @params = grep { length($_) } $q->url_param; return (@params > 0 ? '?' : '').join('&',map { # $q->url_param returns unescaped values so we have to escape them # again sprintf("%s=%s",$_,CGI::escape($q->url_param($_))) } @params); } print $q->header('text/plain'), $q->url(-absolute => 1).url_query_string($q),"\n";

    Testing it with curl (assuming the base URL is localhost:8080/cgi-bin/test.pl):

    $ curl -d foo=bar localhost:8080/cgi-bin/test.pl
    /cgi-bin/test.pl
    $ curl -d foo=bar localhost:8080/cgi-bin/test.pl?msg=hello%20world
    /cgi-bin/test.pl?msg=hello%20world
    

    Hope that helps.

Re: How to get current URL with Query String
by Anonymous Monk on May 15, 2017 at 12:18 UTC
    This is my one line code :

    $page_url = ($ENV{HTTPS} eq "on" || $ENV{HTTP_X_FORWARDED_PROTO} eq "https")?"https://".$ENV{SERVER_NAME}.$ENV{REQUEST_URI}:"http://".$ENV{SERVER_NAME}.$ENV{REQUEST_URI};