I hate this. Everything should be right but just isn't. The line right after "# Show this #!? cookie" displays the cookie fine. But the next request fails... Aaargh. I hate those stupid OO modules. Doing the "ugly hack way" works perfectly, see the second code snippet.

#!/usr/bin/perl use strict; use warnings; use 5.10.0; use Getopt::Long; use JSON::XS; use LWP::UserAgent; use HTTP::Cookies; use Term::ReadKey; use Data::Dumper; my $option; Getopt::Long::Configure('no_ignore_case'); unless ( GetOptions( 'help|h' => \$option->{help}, 'verbose|v' => \$option->{verbose}, 'username|U=s' => \$option->{username}, 'password|W=s' => \$option->{password}, 'api-key|k=s' => \$option->{apikey}, 'hostname|H=s' => \$option->{hostname}, ) ) { die "Fatal: invalid option \n"; } if ( $option->{help} ) { print <<EOL $0 options: --help | -h show this help --verbose | -v verbose --hostname | -H host to connect to (required) --api-key | -k API key to use (required) --username | -U username (required) --password | -W password --archive | -a archive name to search --arch-id | -A archive ID to display --metadata | -m metadata to search for EOL ; exit; } foreach my $param (qw(username apikey hostname)) { die "required option: $param\n" if not $option->{$param}; } if ( not $option->{password} ) { ReadMode('noecho'); print "Enter Password: "; chomp( $option->{password} = <STDIN> ); ReadMode('restore'); say ''; } my $cookie = HTTP::Cookies->new(); my $ua = LWP::UserAgent->new; $ua->ssl_opts( verify_hostname => 0 ); $ua->cookie_jar($cookie); my $credentials = encode_json( { 'login' => $option->{username}, 'password' => $option->{password}, 'apikey' => $option->{apikey} } ); say $credentials if $option->{verbose}; # Authentication my $request = HTTP::Request->new( POST => "https://$option->{hostname}/storiqone-backend/api/v1/auth +/" ); $request->content_type('application/json'); $request->content($credentials); my $result = $ua->request($request); if ( $result->is_success ) { say $result->decoded_content; } else { die "Error: " . $result->decoded_content . "\n" . $result->status_line . "\n"; } # Show this #!? cookie print Dumper $cookie if $option->{verbose}; # list archives $request = HTTP::Request->new( GET => "https://$option->{hostname}/storiqone-backend/api/v1/archi +ve/" ); $request->content_type('application/json'); $result = $ua->request($request); if ( $result->is_success ) { say $result->decoded_content; } else { die "Error: " . $result->decoded_content . "\n" . $result->status_line . "\n"; }

Here is the code snippet without HTTP::Cookies, that just frigging works :

# Save cookie my ( $cookie ) = ($result->header('Set-Cookie') =~ m((PHPSESSID=\w+);) +); say $cookie if $option->{verbose} ; # list archives $request = HTTP::Request->new( GET => "https://$option->{hostname}/storiqone-backend/api/v1/archi +ve/" ); $request->content_type('application/json'); $request->header('Cookie' => $cookie);

In reply to LWP::UserAgent doesn't send my HTTP::Cookies by wazoox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.