accessing parameters in GET response

Discussion in 'Perl Misc' started by John, Sep 13, 2011.

  1. John

    John Guest

    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
     
    John, Sep 13, 2011
    #1
    1. Advertising

  2. John

    J. Gleixner Guest

    On 09/13/11 11:59, John wrote:
    > 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. Gleixner, Sep 13, 2011
    #2
    1. Advertising

  3. John

    John Guest

    "J. Gleixner" <> wrote in message
    news:4e6f5950$0$63199$...
    > On 09/13/11 11:59, John wrote:
    >> 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?


    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
     
    John, Sep 13, 2011
    #3
  4. On 2011-09-13 13:23, J. Gleixner <> wrote:
    > On 09/13/11 11:59, John wrote:
    >> 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...


    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
     
    Peter J. Holzer, Sep 13, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonck van der Kogel
    Replies:
    2
    Views:
    1,011
    Jonck van der Kogel
    May 27, 2004
  2. Jason
    Replies:
    2
    Views:
    560
    Jonathan Mcdougall
    May 13, 2006
  3. kito
    Replies:
    2
    Views:
    443
  4. Tim Olsen
    Replies:
    3
    Views:
    101
    Tim Olsen
    Jul 27, 2006
  5. Florian Loitsch
    Replies:
    11
    Views:
    265
    Michael Winter
    Mar 15, 2005
Loading...

Share This Page