in reply to LWP get Cookies for API

Thanks for the replies I don't think i was that clear.

To use the API i have to login (username and password) using a browser to the url ( blah.com) and then (in chrome) show the cookies and look for the one called AUTH_TOKEN and then have use that token in the curl command. I am trying to find a way to grab that token so i can use it in my perl

Hope that clears it up

Replies are listed 'Best First'.
Re^2: LWP get Cookies for API
by hippo (Archbishop) on Jun 11, 2019 at 13:23 UTC
    my $cookies = HTTP::Cookies->new(); my $ua = LWP::UserAgent->new( cookie_jar => {} );

    ICBW but it looks to me like you are setting up a new cookie jar in $cookies but then failing to associate that with your user agent. The docs for LWP::UserAgent also suggest using a HTTP::CookieJar::LWP instead. Try replacing these lines with

    my $cookies = HTTP::CookieJar::LWP->new; my $ua = LWP::UserAgent->new( cookie_jar => $cookies );

    and see if that gets you any further.

Re^2: LWP get Cookies for API
by Corion (Patriarch) on Jun 11, 2019 at 13:57 UTC

    You're already using Rest::Client to fetch the initial cookie. You should inspect $client->responseHeaders to see where the cookie you want lives. Most likely, it is something like:

    $client->POST('URL', ... credentials ...); my $token = $client->responseHeader('Set-Cookie'); ...

    A better approach is likely to use $ua for fetching the token instead, as that gives you far better control over the headers, and the $cookies cookie jar, if you properly associate it with LWP::UserAgent:

    my $response = $ua->post('URL', Content => ... credentials ...); my $token = $response->headers->{'Set-Cookie'}; # if there is only one + cookie line my $token = $cookies->get_cookies( 'URL', 'AUTH_TOKEN' ); ...