57

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"
jackcogdill
  • 4,343
  • 3
  • 24
  • 45

4 Answers4

108
  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.

that other guy
  • 101,688
  • 11
  • 135
  • 166
  • Thanks that is just what I needed – jackcogdill Feb 20 '14 at 21:26
  • 10
    Doesn't copy cookies in chrome 44 – Dan Schien Aug 28 '15 at 12:19
  • Can't confirm nor disprove on chrome 44. But it definitely works with chrome 45. – jox Sep 03 '15 at 10:19
  • 2
    I have the same issue. Cookies are note being copied and I'm using Chrome 45. I copy them manually by @user2537361 answer below. – Alvaro Oct 02 '15 at 14:01
  • Does not include cookies in Chromium 57.0.2987.98 either. – Nicolas Raoul Apr 26 '17 at 08:22
  • 2
    If the request is in pending state and you copy it as cURL, it doesn't copy cookies. After request finishes with some status, you can copy it as cURL with cookies. I think it is because server might tell to browser delete cookies and if you copy it before finished, you might have old cookies. – Ikrom Sep 17 '18 at 10:56
  • 2
    Chrome 72.0.3626.119 also seems to not copy cookies – Paul Grime Mar 01 '19 at 15:29
  • This is not valid anymore. – revo Oct 06 '19 at 13:43
  • I have confirmed this working on this stackoverflow page as of 2019-10-07 on Chrome 77.0.3865.90. I did the steps from the post, where 3. was to upvote this answer. Chrome sent a request to `https://stackoverflow.com/posts/21919431/vote/2` (shown as `2` in the list), and the cookie tab shows 9 cookies. I copied as cURL and pasted it into an editor, and part of the command is `-H 'cookie: prov=...; _ga=...'` containing a total of 9 fields. – that other guy Oct 07 '19 at 17:35
14

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.
congusbongus
  • 10,870
  • 5
  • 64
  • 86
user2537361
  • 172
  • 3
7

There's an even easier way to do this in Chrome/Chromium.
The open source Chrome extension cookies.txt 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.

Ravinder Payal
  • 2,459
  • 21
  • 38
andDevW
  • 434
  • 7
  • 12
4

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.

Kyle Parisi
  • 706
  • 1
  • 7
  • 14