You don't tell us where your approach fails and how.

Simply converting your curl command line to LWP::UserAgent using curl2lwp gives:

#!perl use strict; use warnings; use WWW::Mechanize; use HTTP::Request; my $ua = WWW::Mechanize->new(); my $r = HTTP::Request->new( 'GET' => 'https://example.com/', [ 'Accept' => '*/*', 'Accept' => 'application/json', 'Authorization' => '<token>', 'Host' => 'example.com:443', 'User-Agent' => 'curl/7.55.1', ], ); my $res = $ua->request( $r, ); __END__ Created from curl command line curl -X GET "https://example.com" -H "accept: application/json" -H "au +thorization:<token>"

If your question is how to extract the cookie from a browser request, have a look at the response you see in the browser.

If your question is how to extract the cookie automatically without involving a browser, first you need to find out what request you have to make to which URL, and also whether sending the username and password as a JSON string in a POST request is the correct approach there. If that is correct, then instead of inspecting the responseContent, inspect the response headers using $response->headers->as_string().

You already mention a variable suggestively named $cookies, but you never pass it anywhere. See the ->cookie_jar method of LWP::UserAgent on how to set a cookie jar for LWP::UserAgent.


In reply to Re: LWP get Cookies for API by Corion
in thread LWP get Cookies for API by abs52

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.