Skip to content

Commit 9e7180c

Browse files
committed
add http_cookie.pl snippet
1 parent 60cb59a commit 9e7180c

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

misc/snippets/http_cookie.pl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use strict;
2+
use warnings;
3+
use HTTP::Tiny;
4+
use HTTP::CookieJar;
5+
6+
# Create a new HTTP::Tiny object
7+
my $http = HTTP::Tiny->new( cookie_jar => HTTP::CookieJar->new );
8+
9+
# Define the URL to request
10+
my $url = 'http://www.test.com';
11+
12+
# Perform a GET request
13+
my $response = $http->get($url);
14+
15+
# Check if the request was successful
16+
if ( $response->{success} ) {
17+
print "Response Status: $response->{status}\n";
18+
print "Response Content:\n$response->{content}\n";
19+
}
20+
else {
21+
warn "Failed to get $url: $response->{status} $response->{reason}\n";
22+
}
23+
24+
# Access the cookie jar
25+
my $cookie_jar = $http->{cookie_jar};
26+
27+
# Print cookies for the domain
28+
my $cookies = $cookie_jar->cookies_for($url);
29+
if ($cookies) {
30+
print "Cookies:\n";
31+
foreach my $cookie ( keys %$cookies ) {
32+
print "$cookie: $cookies->{$cookie}\n";
33+
}
34+
}
35+
else {
36+
print "No cookies\n";
37+
}

0 commit comments

Comments
 (0)