Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

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

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



  • 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 about the Monastery: (5)
As of 2024-03-29 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found