Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LWP params Best practice 1

Status
Not open for further replies.

Corwinsw

Programmer
Sep 28, 2005
117
BG
Hi.
Can you tell me what is the best practice to compose and escape params, when using LWP.
Currently I am doing that:
Code:
   my $params     = shift;

   my $params_as_str;
   if ( ref $params eq 'HASH' && $params ) {
      foreach my $k ( keys %{$params} ) {
         $params_as_str .= "$k=$params->{$k}&";
      }
      chop $params_as_str;
   }
which is really ugly and I have forgotten to escape the params(I think that there is a way automatically to do that).

Corwin
 
Code:
use URI::Escape;

my $data = {
	username => 'soandso',
	password => 'big secret',
	hashcode => 'fdsfsdfsdfdfds87f6s8df6',
};

my @fields = ();
while (($key,$value) = each %{$data}) {
	push (@fields,join('=',$key,uri_escape($value)));
}
my $params = join ('&',@fields);

print "$params\n";
 
Code:
$uri = URI->new( $url );
$uri->query_form( $query_params );
my $req = HTTP::Request->new( POST=>$uri );
$ua->request($req);

Where the $query_params is a hash reference with the names and values of the params.

Corwin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top