AHA! I knew it would something simple...Thank you for taking the time to
point that out. For whatever it is worth, here is the working code: Now,
I will make it a tad more robust by ensuring that I handle expired cookies
by re-authenticating...
Thanks again!
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use Carp qw(cluck carp croak);
use Data::Dumper;
use File::Tail;
use Proc::ProcessTable;
use Mojolicious;
use Mojo::UserAgent;
use Mojo::UserAgent::CookieJar;
my $h = 'someHost';
my $ua = Mojo::UserAgent->new;
$ua->inactivity_timeout(600);
my $jar = Mojo::UserAgent::CookieJar->new;
doLogin($h,'user','pass');
my $query = <<QUERY;
CONSTRUCT {
?s ?p ?o .
}
WHERE {
?s ?p ?o .
}
LIMIT 10
QUERY
my $tx = $ua->post('someHost/SPARQL/query' => 'form' => {'query' => $query,
'output' => 'nt'});
say $tx->res->body;
sub doLogin {
my ($host,$user,$pass) = @_;
my $loginUrl = 'https://'.$host.'/dologinCmd.html';
my $tx = $ua->post($loginUrl => 'form' => {'httpd_username' => $user,
'httpd_password' => $pass});
if(defined
$tx->res->content->headers->{'headers'}->{'location'}->[0]->[0] and
$tx->res->content->headers->{'headers'}->{'location'}->[0]->[0] eq
'/success.html') {
$jar->extract($tx);
return
@{$tx->res->content->headers->{'headers'}->{'set-cookie'}[0]};
}
return 1;
}
On Thu, Jun 19, 2014 at 12:13 AM, [email protected] <[email protected]>
wrote:
> Maybe you're looking for
> https://metacpan.org/pod/Mojo::UserAgent#cookie_jar
> Use one global UserAgent object for all queries - define it outside
> doLogin (now you're creating a new one in the doLogin sub and it dies just
> after result is returned)
>
> All,
>
> Please forgive the potentially easy answer...but it is escaping me at
> the moment. I have to deal with a stupid form-based Auth mechanism in
> order to access an internal service (SPARQL endpoint).
>
> I have written the code necessary to extract the cookie once I send the
> login info to the right URL...But, in order to do follow-on queries, I need
> to add the resulting cookie to the header of the POST that I send back.
>
> How do I do this? My sub to grab the cookie is here...
>
> my $h = 'someHost.com';
>
> say doLogin($h,'user','pass');
>
>
> sub doLogin {
> my ($host,$user,$pass) = @_;
> my $loginUrl = 'https://'.$host.'/dologinCmd.html';
> my $ua = Mojo::UserAgent->new;
> my $tx = $ua->post($loginUrl => 'form' => {'httpd_username' => $user,
> 'httpd_password' => $pass});
>
> if(defined
> $tx->res->content->headers->{'headers'}->{'location'}->[0]->[0] and
> $tx->res->content->headers->{'headers'}->{'location'}->[0]->[0] eq
> '/success.html') {
> return
> @{$tx->res->content->headers->{'headers'}->{'set-cookie'}[0]};
> }
> return 'error';
> }
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.