accessing parameters in GET response

J

John

I have

use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $response = $ua->get($url);
my $content = $response->content;

I can get the parameters values a=b c=d etc by @parts=split(/\&/,$content)
etc etc

but is there an easier way?

Regards
John
 
J

J. Gleixner

I have

use LWP::UserAgent;
my $ua = new LWP::UserAgent;
my $response = $ua->get($url);
my $content = $response->content;

I can get the parameters values a=b c=d etc by @parts=split(/\&/,$content)
etc etc

but is there an easier way?

Seems like a odd value for $content...

Also, if that does work, what could be easier than a 5 line program and
calling split? (BTW: no need to escape it.)

What are you really trying to do?
 
J

John

J. Gleixner said:
Seems like a odd value for $content...

Also, if that does work, what could be easier than a 5 line program and
calling split? (BTW: no need to escape it.)

What are you really trying to do?

Yes, you're right it's only 5 lines. I was thinking whether there existed a
one liner - perhaps part of LWP or CGI.

Thanks.
John
 
P

Peter J. Holzer

Seems like a odd value for $content...

Content-Type: application/x-www-form-urlencoded

This is normal for the body of a request, but you are right, it is very
unusual for the body of a response. (Could be part of an API, though -
easier to parse than XML or JSON)

Also, if that does work, what could be easier than a 5 line program and
calling split? (BTW: no need to escape it.)

If it really is application/x-www-form-urlencoded, split alone isn't
sufficient. You have to do something like (WARNING: Untested code):

for my $part (split(/&/, $content)) {
my ($k, $v) = split(/=/, $part, 2);
s/=([0-9A-F])/chr(hex($1))/e for ($k, $v);
$_ = decode('UTF-8', $_) for ($k, $v); # if params in UTF-8
push @params, [$k, $v]; # or use HoA
}

Interestingly, the CGI module doesn't seem to have a nice, exposed
interface for this stuff (although there are few more or less obvious
hacks to get it to parse urlencoded strings).

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top