Examples & Tutorials ๐ โ
This guide provides comprehensive examples demonstrating real-world usage of get-cookie. All examples are available in the examples/ directory of the repository.
Quick Start Examples โ
For immediate practical patterns, see the Quick Start Guide:
# View the quick start examples
./examples/quick-start.shThe quick start guide covers:
- Basic cookie extraction patterns
- One-liner curl commands
- Reusable shell functions for your
.bashrc/.zshrc - Common output formats
- Quick login status checks
Example File: examples/quick-start.sh
curl Integration Examples โ
For comprehensive curl integration patterns, see the curl Integration Guide:
# View comprehensive curl integration examples
./examples/curl-integration.shThis guide demonstrates:
- Basic single cookie extraction with curl
- The perfect pattern using
--urlflag - Multiple cookie authentication
- Downloading protected content
- POST requests with authentication
- Cookie validation functions
- Integration with wget, HTTPie, and other tools
Example File: examples/curl-integration.sh
Key Patterns from curl Integration โ
The Perfect One-Liner โ
# Automatically get all cookies for a URL
curl -H "Cookie: $(get-cookie --url <URL> --render)" <URL>Reusable Shell Function โ
# Add to your .bashrc/.zshrc
authenticated_curl() {
local url=$1
shift
curl -H "Cookie: $(get-cookie --url "$url" --render 2>/dev/null)" "$@" "$url"
}
# Usage
authenticated_curl https://github.com/settings/profile -s | grep usernameGitHub Authentication Examples โ
For GitHub-specific authentication patterns, see the GitHub Authentication Guide:
# View GitHub authentication examples
./examples/github-auth.shThis guide covers:
- Smart cookie filtering for valid sessions
- JSON filtering for valid cookies
- Expired cookie filtering comparison
- Systematic endpoint testing
- Private repository access patterns
- Web vs API authentication clarification
Example File: examples/github-auth.sh
Important: Web vs API Authentication โ
Browser cookies work for GitHub web pages, NOT the REST API:
โ Works with browser cookies:
- Viewing private repositories in browser
- Accessing settings pages
- Web scraping authenticated content
โ Does NOT work with browser cookies:
- GitHub REST API (
api.github.com) - GitHub GraphQL API
- Git operations (clone, push, pull)
For API access, use:
- GitHub CLI:
gh auth login - Personal Access Tokens
- OAuth apps
GitHub Authentication Patterns โ
Method 1: Smart Cookie Filtering (Recommended) โ
# Using --url and --render with automatic filtering
COOKIES=$(get-cookie --url https://github.com/settings/profile --render)
curl -H "Cookie: $COOKIES" https://github.com/settings/profileMethod 2: JSON Filtering for Valid Cookies โ
# Get valid session cookie (filter by length)
VALID_SESSION=$(get-cookie --url https://github.com --output json | \
jq -r '.[] | select(.name == "user_session" and (.value | length) > 20) | .value' | \
head -1)
# Build cookie header
COOKIE_HEADER="user_session=$VALID_SESSION; __Host-user_session_same_site=$VALID_SESSION"
curl -H "Cookie: $COOKIE_HEADER" https://github.com/settings/profileCLI Features Examples โ
For comprehensive demonstrations of all CLI features, see the Features Demo:
# View all CLI features
./examples/features-demo.shThis guide demonstrates:
- Profile listing and selection with
--list-profiles - Cookie deduplication (automatic and manual)
- Expired cookie filtering
- Browser-specific extraction
- Combining multiple features
- Practical workflows
Example File: examples/features-demo.sh
Key Features Demonstrated โ
Profile Discovery โ
# List available Chrome profiles
get-cookie --browser chrome --list-profiles
# Use a specific profile
get-cookie --url https://github.com --browser chrome --profile "Work" --renderCookie Deduplication โ
# Default: automatically deduplicates (keeps longest/most valid)
get-cookie user_session github.com --render
# Include all duplicates (for debugging)
get-cookie user_session github.com --include-all --output jsonExpired Cookie Filtering โ
# Default: filters expired cookies
get-cookie % github.com --output json
# Include expired cookies (for audit)
get-cookie % github.com --include-expired --output jsonDevelopment Examples โ
For development and testing, see the CLI Examples:
# View development examples
./examples/cli-examples.shThis file demonstrates basic CLI usage patterns for development and testing.
Example File: examples/cli-examples.sh
TypeScript Examples โ
For programmatic usage, see the TypeScript examples:
examples/basic-usage.ts: Basic Node.js module usageexamples/advanced-usage.ts: Advanced usage with browser-specific strategiesexamples/comprehensive-demo.ts: Comprehensive demonstration of all featuresexamples/auth-tokens.ts: Authentication token extraction patterns
Running the Examples โ
Prerequisites โ
Install get-cookie globally or ensure it's in your PATH:
bashnpm install -g @mherod/get-cookie # or pnpm add -g @mherod/get-cookieMake scripts executable:
bashchmod +x examples/*.shEnsure required tools are installed:
curl(for HTTP examples)jq(for JSON processing)- Browser with cookies (Chrome, Firefox, or Safari)
Running Shell Script Examples โ
# Quick start
./examples/quick-start.sh
# curl integration
./examples/curl-integration.sh
# GitHub authentication
./examples/github-auth.sh
# CLI features
./examples/features-demo.shRunning TypeScript Examples โ
# Install dependencies
pnpm install
# Run TypeScript examples
pnpm tsx examples/basic-usage.ts
pnpm tsx examples/advanced-usage.ts
pnpm tsx examples/comprehensive-demo.ts
pnpm tsx examples/auth-tokens.tsExample Use Cases Covered โ
Authentication & Session Management โ
- โ Extracting authentication tokens
- โ Managing multiple browser profiles
- โ Session cookie analysis
- โ GitHub authentication patterns
API Testing & Development โ
- โ curl integration patterns
- โ Multiple cookie handling
- โ POST requests with authentication
- โ Cookie validation
Security & Compliance โ
- โ Cookie deduplication
- โ Expired cookie filtering
- โ Security token extraction
- โ Cookie auditing
Automation & CI/CD โ
- โ Automated testing patterns
- โ Continuous monitoring
- โ Error handling patterns
- โ Shell script automation
Best Practices from Examples โ
1. Always Use --url Flag for curl Integration โ
# โ
Recommended
curl -H "Cookie: $(get-cookie --url <URL> --render)" <URL>
# โ Not recommended (requires knowing domain and cookie names)
curl -H "Cookie: $(get-cookie user_session github.com --render)" <URL>2. Filter for Valid Cookies โ
# Filter by length to avoid truncated/invalid cookies
VALID_SESSION=$(get-cookie --url https://github.com --output json | \
jq -r '.[] | select(.name == "user_session" and (.value | length) > 20) | .value' | \
head -1)3. Use Profile Selection to Avoid Conflicts โ
# List profiles first
get-cookie --browser chrome --list-profiles
# Then use specific profile
get-cookie --url <URL> --browser chrome --profile "Profile Name" --render4. Handle Errors Gracefully โ
# Always check for empty results
COOKIES=$(get-cookie --url <URL> --render 2>/dev/null)
if [ -z "$COOKIES" ]; then
echo "No cookies found" >&2
exit 1
fiRelated Documentation โ
- CLI Usage Guide - Complete CLI reference
- Use Cases - Real-world use case patterns
- Shell Script Automation - Automation patterns
- Troubleshooting - Common issues and solutions
- Security Guide - Security best practices
Contributing Examples โ
If you have a useful example pattern, consider contributing it to the examples directory. See the Contributing Guide for details.