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

Hello Team, I was trying to capture the "Link" value contains string "next" in order to do the pagination but i was unable to do the same as i was collecting from header-as-string from LWP request. How i can collect the "Link" value that contains "next"?

Please check my header contents as print value

Cache-Control: no-cache, no-store Connection: close Date: Fri, 26 Mar 2021 02:11:16 GMT Pragma: no-cache Server: nginx Vary: Accept-Encoding Content-Type: application/json Expires: 0 Client-Date: Fri, 26 Mar 2021 02:11:16 GMT Client-Peer: 10.10.1.32:8080 Client-Response-Num: 1 Client-Transfer-Encoding: chunked Expect-Ct: report-uri="https://org.extract.net", max-age=0 Link: <https://sso.myorg.com/api/v5/myids?limit=200>; rel="self" Link: <https://sso.myorg.com/api/v5/myids?after=200xxxxxxxxxxxxxxxxx&l +imit=200>; rel="next"

my code portion

use LWP; use LWP::UserAgent; $response1 = $response->headers_as_string; print "$response1\n";

Please check below portion that is blank

$linkheader = $response1=~m/Link/; print "$linkheader\n";

How i can collect the Link values from the data above?

Replies are listed 'Best First'.
Re: Perl to collect "Link" values contains "next"
by Corion (Patriarch) on Mar 26, 2021 at 06:46 UTC

    >See HTTP::Headers. Don't convert them to a string and then try to parse out the >Link, but access it directly, using the ->header() method:

    #!perl use warnings; use strict; use HTTP::Headers; use 5.012; my $headers = HTTP::Headers->new(); $headers->header( 'Cache-Control' => 'no-cache, no-store', 'Connection' => 'close', 'Date' => 'Fri, 26 Mar 2021 02:11:16 GMT', 'Pragma' => 'no-cache', 'Server' => 'nginx', 'Vary' => 'Accept-Encoding', 'Content-Type' => 'application/json', 'Expires' => '0', 'Client-Date' => 'Fri, 26 Mar 2021 02:11:16 GMT', 'Client-Peer' => '10.10.1.32:8080', 'Client-Response-Num' => '1', 'Client-Transfer-Encoding' => 'chunked', 'Expect-Ct' => 'report-uri="https://org.extract.net", max-age=0', 'Link' => '<https://sso.myorg.com/api/v5/myids?limit=200>; rel="se +lf"', 'Link' => '<https://sso.myorg.com/api/v5/myids?after=200xxxxxxxxxx +xxxxxxx&limit=200>; rel="next"', ); say "Found link headers:"; for my $h ( $headers->header('Link') ) { if( $h =~ /\brel="next"/ ) { say "Found 'next' link: $h"; } else { say "Ignoring other Link header: $h"; }; }; __END__ Found link headers: Ignoring other Link header: <https://sso.myorg.com/api/v5/myids?limit= +200>; rel="self" Found 'next' link: <https://sso.myorg.com/api/v5/myids?after=200xxxxxx +xxxxxxxxxxx&limit=200>; rel="next"

      Thanks team I was able to resolve my pagination issue below below object in the sub function in my code

      $client->request(HTTP::Request->new('GET', "$_[0]"));

      and resolved the the Link next function

      $linkheader = $response->header('Link'); if ($linkheader=~ m/next/) {......}
Re: Perl to collect "Link" values contains "next"
by hippo (Archbishop) on Mar 26, 2021 at 10:47 UTC

    Corion's approach is the correct one. However, since you asked using a regex on the string and have still not shown that you have learned How to ask better questions using Test::More and sample data, here is one final demonstration.

    use strict; use warnings; use Test::More tests => 1; my $response1 = <<EOT; Cache-Control: no-cache, no-store Connection: close Date: Fri, 26 Mar 2021 02:11:16 GMT Pragma: no-cache Server: nginx Vary: Accept-Encoding Content-Type: application/json Expires: 0 Client-Date: Fri, 26 Mar 2021 02:11:16 GMT Client-Peer: 10.10.1.32:8080 Client-Response-Num: 1 Client-Transfer-Encoding: chunked Expect-Ct: report-uri="https://org.extract.net", max-age=0 Link: <https://sso.myorg.com/api/v5/myids?limit=200>; rel="self" Link: <https://sso.myorg.com/api/v5/myids?after=200xxxxxxxxxxxxxxxxx&l +imit=200>; rel="next" EOT my $want = '<https://sso.myorg.com/api/v5/myids?after=200xxxxxxxxxxxxx +xxxx&limit=200>; rel="next"'; my @linkheader = $response1 =~ /^Link: (.*"next")/mg; is $linkheader[0], $want;

    🦛

      Frankly my reply is not receiving enough negative votes as it was not directly related, and might have sent OP chasing red herrings. Come on, people!

Re: Perl to collect "Link" values contains "next"
by parv (Parson) on Mar 26, 2021 at 02:42 UTC