How to read request parameter value from URL using perl command

In the shell, I need to extract a specific request parameter from the URI.

I tried playing around with this to get the offset value

echo "/mypath/index.php?offset=20&query=uro" | perl -MURI -le 'chomp($url = <>); print URI->new($url)->query_form("offset")'

But he always returns only offset=20&query=uro

Please, help

+5
source share
3 answers

query_form returns a hash, change your script to:

perl -MURI -le 'chomp($url = <>); print +{URI->new($url)->query_form}->{offset}'

To process multiple lines:

perl -MURI -nle 'print +{URI->new($_)->query_form}->{offset}'
+4
source

You can use URI::QueryParamin addition to URI. The method in the module gives you the values ​​of the request parameters. query_paramURI::QueryParam

echo "/mypath/index.php?offset=20&query=uro" | perl -MURI -le 'use URI::QueryParam; chomp($url = <>); print URI->new($url)->query_param(offset);'
+3
source

You can use the module CGI:

perl -MCGI=param -e 'print param("offset")' "index.php?offset=20&query=uro"
+1
source

All Articles