I am giving a http request through perl script using LWP::UserAgent and HTTP::Request. I have to pass cookie information along with the request. Doesn anybody know how to do that? Is cookie_jar the option? can anybody share some example? TIA
HTTP::Cookies
Tried this as well but not able to get intended results. Actually i am trying to do the teamsite authentication through scripting and passing below information in the cookie but not able to successfully authenticate and in the response content, i see Login screen (ideally i should see the actual page which i calling through http:request and not Login screen). I hacked the Auth and Jession information from browser session on the server.IWAUTH => '23523e646f6d4956724971f5b96454c5e8b606799f180cca255c55bcaf7445eae862da9e70216ae71e07b375157385d1d91d809ee483e96db9e1ad899c6dfd15c',iw_domain => 'domainname',iw_which_ui => '',iw_user => 'username',JSESSIONID =>'REFEBFA3D0656A6F5E8A88E26331EA23'Any thoughts if i need to try something else?
Passing the auth and session tokens won't work. You script will stop working a short time later, as soon as the session expires. What you'd want to do is actually spoof the login starting from the login page, passing in a real username and password. Yes it can be done, but there's probably better ways of achieving this. One is using a Java class and/or JSP, since those can be wrapped with IWOV's authentication filter. The other, is to have the PERL script read the cookies passed by your incoming user (assuming you're using a CGI screen), and pass those on to the inner HTTP request.
Here is what i did. Using the CGI Screen, captured the cookie information and passed it on to my http request. But still not able to authenticate. Below is my code.[html]foreach $key (sort keys %{$cgi->{cookie}}) { $cookie_jar = $cookie_jar.$key." => '".$cgi->{cookie}{$key}."' , ";}chop($cookie_jar);chop($cookie_jar);$ua = LWP::UserAgent->new;$request = HTTP::Request->new(GET => 'http://servername/URL');$ua->cookie_jar($cookie_jar);$response = $ua->request($request);print $response->content;[/html]
That does not look right. After you execute your 'foreach' loop your $cookie_jar will be a string, not an Object of HTTP::Cookies Class.One way or another, you'll have to put cookies into cookie jar. For example, you may use "set_cookie" method. Something like this:[php]use HTTP::Cookies;my $cookie_jar = HTTP::Cookies->new( autosave => 0,);. . .foreach my $key (sort keys %{$cgi->{cookie}}) { $cookie_jar->set_cookie( 1, # version $key, # key $cgi->{cookie}{$key}, # value '/', # path 'YourTSDomain.com', # domain '80' # port # Few more optional parameters if needed );};[/php]
Also is there a way for me to validate the what exactly was sent in request?
Sure is. Send it to your own test cgi that does nothing but logs required info. Or send it to OTB show_env.cgi and analyze the output
For capturing the response, thats exactly what i am doing. Using Show_env.cgi to display the response. But this is for capturing the response and not for capturing the sent request. Is there a way to check that?
Ok, I'll try one more time....In your LWP code, temporarily modify URL to target show_env.cgi. Set up your cookies, Form, whatever. Send a request. Note - direct show_env request will go through, you do not have to authenticate! On the server side, WebServer + show_env will process the request, format theresponse showing what was in the request (plus Env Variables) and send it back. You can then analyze your $response->content and see what you originally sent. You can for example save response as html File and open it with the Browser.When satisfied, restore original URL and bingo! Now you know exactly what will be posted to thatTarget when you send a request from your LWP code.Kapish?
i think i found out the issue...
Doubt it. Something is fishy here...The process is local. As far as Cookies are concerned, Agent does not use Server and does not care whetherfully qualified host name is really a Domain, Server name, if it has a DNS Entry or anything else. For all Agent cares it's a String. If Cookie's Domain matches Request's URL, the Cookie goes period.To elaborate a little, Cookies are set by the Agent and sent through Cookie HTTP header. To dothat, complete Cookie Jar is scanned to get cookies that "match" Request's URL. Basically, URL's hostname should match exactly and both URL resources (right-to-left) and URL Path (left-to-right) partiallyfor the cookie to be considered a match. The better match goes. If there are several equally good matches,all of them go.
A big time thanks to you ! I am able to login to Teamsite through script now and it resolved a big technical issue for me. Above issue is non issue for me as i was able to access cookies through the domain name once it was configured. Why it was not working through server name, i dont know and i wouldnt even be bothered as i have the alternate solution working (using domain name).Again thanks for the help !