How do I copy cookies from Chrome?

BashGoogle ChromeCookiesCurl

Bash Problem Overview


I am using bash to to POST to a website that requires that I be logged in first. So I need to send the request with login cookie. So I tried logging in and keeping the cookies, but it doesn't work because the site uses javascript to hash the password in a really weird fashion, so instead I'm going to just take my login cookies for the site from Chrome. How do get the cookies from Chrome and format them for Curl?

I'm trying to do this:

curl --request POST -d "a=X&b=Y" -b "what goes here?" "site.com/a.php"

Bash Solutions


Solution 1 - Bash

  1. Hit F12 to open the developer console (Mac: Cmd+Opt+J)
  2. Look at the Network tab.
  3. Do whatever you need to on the web site to trigger the action you're interested in
  4. Right click the relevant request, and select "Copy as cURL"

This will give you the curl command for the action you triggered, fully populated with cookies and all. You can of course also copy the flags as a basis for new curl commands.

Solution 2 - Bash

In Chrome:

  • Open web developer tools (view -> developer -> developer tools)
  • Open the Application tab (on older versions, Resources)
  • Open the Cookies tree
  • Find the cookie you are interested in.

In the terminal

  • add --cookie "cookiename=cookievalue" to your curl request.

Solution 3 - Bash

There's an even easier way to do this in Chrome/Chromium.
The open source Chrome extension [cookies.txt][1] exports cookie data in a cookies.txt file, and generates an optional ready-made wget command.

*I have nothing to do with the extension, it just works really well.

[1]: https://chrome.google.com/webstore/detail/get-cookiestxt/bgaddhkoddajcdgocldbbfleckgcbcid/related?hl=en "cookies.txt"

Solution 4 - Bash

I was curious if others were reporting that chrome doesn't allow "copy as curl" feature to have cookies anymore.

It then occurred to me that this is like a security idea. If you visit example.com, copying requests as curl to example.com will have cookies. However, copying requests to other domains or subdomains will sanitize the cookies. a.example.com or test.com will not have cookies for example.

Solution 5 - Bash

Can't believe no one has mentioned this. Here's the easiest and quickest way.

Simply open up your browser's Developer Tools, click on the Console tab, and lastly within the console, simply type the following & press ENTER...

console.log(document.cookie)

The results will be immediately printed in proper format. Simply highlight it and copy it.

Solution 6 - Bash

For anyone that wants all of the cookies for a site, but doesn't want to use an extension:

  • Open developer tools -> Application -> Cookies.
  • Select the first cookie in the list and hit Ctrl/Cmd-A
  • Copy all of the data in this table with Ctrl/Cmd-C

Now you have a TSV (tab-separated value) string of cookie data. You can process this in any language you want, but in Python (for example):

import io
import pandas as pd

cookie_str = """[paste cookie str here]"""

# Copied from the developer tools window.
cols = ['name', 'value', 'domain', 'path', 'max_age', 'size', 'http_only', 'secure', 'same_party', 'priority']

# Parse into a dataframe.
df = pd.read_csv(io.StringIO(cookie_str), sep='\t', names=cols, index_col=False)

Now you can export them in Netscape format:

# Fill in NaNs and format True/False for cookies.txt.
df = df.fillna(False).assign(flag=True).replace({True: 'TRUE', False: 'FALSE'})
# Get unix timestamp from max_age
max_age = (
    df.max_age
    .replace({'Session': np.nan})
    .pipe(pd.to_datetime))
start = pd.Timestamp("1970-01-01", tz='UTC')
max_age = (
    ((max_age - start) // pd.Timedelta('1s'))
    .fillna(0)  # Session expiry are 0s
    .astype(int))  # Floats end with ".0"
df = df.assign(max_age=max_age)

cookie_file_cols = ['domain', 'flag', 'path', 'secure', 'max_age', 'name', 'value']
with open('cookies.txt') as fh:
  # Python's cookiejar wants this header.
  fh.write('# Netscape HTTP Cookie File\n')
  df[cookie_file_cols].to_csv(fh, sep='\t', index=False, header=False)

And finally, back to the shell:

# Get user agent from navigator.userAgent in devtools
wget -U $USER_AGENT --load-cookies cookies.txt $YOUR_URL

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjackcogdillView Question on Stackoverflow
Solution 1 - Bashthat other guyView Answer on Stackoverflow
Solution 2 - Bashuser2537361View Answer on Stackoverflow
Solution 3 - BashandDevWView Answer on Stackoverflow
Solution 4 - BashKyle ParisiView Answer on Stackoverflow
Solution 5 - Bashsudo soulView Answer on Stackoverflow
Solution 6 - BashkristinaView Answer on Stackoverflow