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

why if i post like this it doesn't work

$name = 'john'; my $req = HTTP::Request->new(GET => 'https://www.google.com/?name=$nam +e'); $req->content_type('application/json');

but it works when no $name string in url

my $req = HTTP::Request->new(GET => 'https://www.google.com/?name=john +'); $req->content_type('application/json');

Replies are listed 'Best First'.
Re: url get with string
by huck (Prior) on Mar 23, 2017 at 08:38 UTC

    inside single quotes the $ is just more text, not the start of a variable name

    Try

    my $req = HTTP::Request->new(GET => "https://www.google.com/?name=$nam +e");
    or
    my $req = HTTP::Request->new(GET => 'https://www.google.com/?name='.$n +ame);

      i tried it bt failed. lets say if the link is like this

      $fname = "john"; $lname = "doe"; $ctry = "canada"; my $req = HTTP::Request->new(GET => 'https://www.google.com/?firstname +=$fname&lastname=$lname&country=$ctry')

      but works with no $ string

      my $req = HTTP::Request->new(GET => 'https://www.google.com/?firstname=john&lastname=deo&country=canada')

        Try splitting up your problem in two parts:

        $fname = "john"; $lname = "doe"; $ctry = "canada"; my $url = 'https://www.google.com/?firstname=$fname&lastname=$lname&co +untry=$ctry'; print "Requesting ", $url, "\n"; my $req = HTTP::Request->new(GET => $url);

        Also, you might want to learn about single and double quotes and how they differ in behaviour:

        my $single_quoted = 'https://www.google.com/?firstname=$fname&lastname +=$lname&country=$ctry'; my $double_quoted = "https://www.google.com/?firstname=$fname&lastname +=$lname&country=$ctry"; print 'Single quotes:', $single_quoted, "\n"; print "Double quotes:", $double_quoted, "\n";

        It fails because you are still using single quotes.

Re: url get with string
by haukex (Archbishop) on Mar 23, 2017 at 08:49 UTC

    If $name is just a simple string like you showed, with no special characters, then huck has given you the answer in regards to interpolation in '$name' vs. "$name".

    However, if you've got any special characters in that string, they need to be escaped properly. Here's one way with URI:

    use URI; my $name = 'xyz&foo=bar'; my $uri = URI->new('https://www.google.com/'); $uri->query_form( name => $name, foo => 'quz', ); print "$uri\n"; __END__ https://www.google.com/?name=xyz%26foo%3Dbar&foo=quz

      haukex am using LWP::UserAgent, guys non of idea has worked,most guys dont believe that some url dont accept using string in url they only allow normal form. so u just have to bypass or force it. any better idea i appreciate