Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-19 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found