72

I am using curl to retrieve cookies like so:

curl -c cookies.txt url

then I parse the cookie I want from the cookies.txt file and send the request again with the cookie

curl -b "name=value" url 

Is this the correct way to send the cookie? Is there a simpler way?

j0k
  • 22,600
  • 28
  • 79
  • 90
dmz73
  • 1,588
  • 4
  • 20
  • 32

4 Answers4

104

You can use -b to specify a cookie file to read the cookies from as well.

In many situations using -c and -b to the same file is what you want:

curl -b cookies.txt -c cookies.txt http://example.com

Further

Using only -c will make curl start with no cookies but still parse and understand cookies and if redirects or multiple URLs are used, it will then use the received cookies within the single invoke before it writes them all to the output file in the end.

The -b option feeds a set of initial cookies into curl so that it knows about them at start, and it activates curl's cookie parser so that it'll parse and use incoming cookies as well.

See Also

The cookies chapter in the Everything curl book.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • 7
    I wish that the man page for `--cookie-jar` didn't say "This command line option will activate the cookie engine that makes curl record **and use** cookies." [emphasis mine] Since that makes it sound like just `--cookie-jar` will lead to cookies being sent, which is false. – blahdiblah Mar 28 '14 at 00:19
  • 1
    @blahdiblah: it's true. if you use --cookie-jar for a command line that first receives cookies then follow a redirect to a second page, it will use the cookies from the first page (assuming they match etc). It just won't read any initial cookies from any file. – Daniel Stenberg Mar 28 '14 at 06:59
  • Yes, the man page is misleading. I thought I was sending cookies just by using the -c option as well until I used -v and saw that it wasn't sending them. – deltaray Jun 26 '14 at 15:16
  • 1
    Could you share the format of the cookies.txt? How does it look like? – macemers Jun 03 '15 at 09:21
  • 1
    man file is clearer now: `NOTE that the file specified with -b, --cookie is only used as input. No cookies will be stored in the file. To store cookies, use the -c, --cookie-jar option or you could even save the HTTP headers to a file using -D, --dump-header!` – ErichBSchulz Apr 28 '16 at 03:31
17

.example.com TRUE / FALSE 1560211200 MY_VARIABLE MY_VALUE

The cookies file format apparently consists of a line per cookie and each line consists of the following seven tab-delimited fields:

  • domain - The domain that created AND that can read the variable.
  • flag - A TRUE/FALSE value indicating if all machines within a given domain can access the variable. This value is set automatically by the browser, depending on the value you set for domain.
  • path - The path within the domain that the variable is valid for.
  • secure - A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
  • expiration - The UNIX time that the variable will expire on. UNIX time is defined as the number of seconds since Jan 1, 1970 00:00:00 GMT.
  • name - The name of the variable.
  • value - The value of the variable.

From http://www.cookiecentral.com/faq/#3.5

Agustí Sánchez
  • 10,455
  • 2
  • 34
  • 25
11

if you have Firebug installed on Firefox, just open the url. In the network panel, right-click and select Copy as cURL. You can see all curl parameters for this web call.

the
  • 21,007
  • 11
  • 68
  • 101
reachlin
  • 4,516
  • 7
  • 18
  • 23
1

Very annoying, no cookie file exmpale on the official website https://ec.haxx.se/http/http-cookies.

Finnaly, I find it does not work, if your file content is just copyied like this

foo1=bar;foo2=bar2

I gusess the format must looks the style said by @Agustí Sánchez . You can test it by -c to create a cookie file on a website.

So try this way, it works

curl -H "Cookie:`cat ./my.cookie`"   http://xxxx.com

You can just copy the cookie from chrome console network tab.

Victor Choy
  • 4,006
  • 28
  • 35