cURL cookie syntax (from bash CLI, not cookie file)

16,116

I was curious to see whether this would be a problem on my server and what I would need to solve it.

First thing was I tested using a browser, to study the baseline behavior.

To start, I connected to make sure no cookies were set yet:

http://hostname/test/testcookie.php

Cookie: array(0) {
}

Then I connected to my script to set a cookie.

http://hostname/test/setcookie.php

Cookie set.

And then I checked to make sure the browser was sending that new cookie:

http://hostname/test/testcookie.php

Cookie: array(1) {
  ["cookiename"]=>
  string(11) "cookievalue"
}

Once I had verified the browser/server behavior, it was time to make things happen from the command line using curl.

First we'll see what happens when we don't try and send a cookie to the server:

$ curl -s "http://hostname/test/testcookie.php"

And here is the result:

Cookie: array(0) {
}

Next we'll see what happens when we do send a cookie, using --cookie as mentioned in the question:

$ curl -s --cookie "cookiename=cookievalue" "http://hostname/test/testcookie.php"

Cookie: array(1) {
  ["cookiename"]=>
  string(11) "cookievalue"
}

And this demonstrates that the --cookie parameter does work and perhaps the OP has other issues going on that might need some debugging.

So now, just for fun, let's see what happens when we use a cookie file and call the cookie setting script:

$ curl -s --cookie-jar /tmp/cookiefile "http://hostname/test/setcookie.php"

Cookie set.

Here's what that cookie looks like in the file:

hostnametabFALSEtab/test/tabFALSEtab1333484771tabcookienametabcookievalue

And now we'll call the cookie tester and use the cookie file this time.

$ curl -s --cookie /tmp/cookiefile "http://hostname/test/testcookie.php"

Cookie: array(1) {
  ["cookiename"]=>
  string(11) "cookievalue"
}

I hope this helps with debugging cookie issues that anybody might have.


And here are the files I used on my server, in my /test path.

Here is /www/test/testcookie.php:

<?php
header("Content-type: text/plain");
echo "Cookie: ";
var_dump($_COOKIE);
?>

And here is /www/test/setcookie.php:

<?php
header("Content-type: text/plain");
setcookie("cookiename", "cookievalue", time()+86400 );
?>
Cookie set.
Share:
16,116
JT Gray
Author by

JT Gray

Updated on June 04, 2022

Comments

  • JT Gray
    JT Gray almost 2 years

    I have a web server that returns to me the same cookie value which I sent it in my request. For this, I have been using the --cookie file successfully (minus a minor speed bump originating with a tabs to spaces issue in the cookie file).

    Unfortunately, if I run the same command but with the cookie name and value in the command rather than in the cookie file, my server does not respond with the desired results.

    According to the cURL docs, something like this should work:

    curl --cookie 'cookiename=cookievalue' --cookie-jar - http://my.site.com/page/with/cookies -v

    But it does not. However, if I use the tab-delimited cookie file instead of the parameters in the command line, it works successfully. What's more is that I have tried pretty much every permutation of these cookie params at the CLI to no avail: tab-delimited, name=cookiename;value=cookievalue(etc), with commas, with the path, as --header, etc etc.

    When I compare the outbound requests side-by-side, I see no apparent difference between the successful and unsuccessful requests, but the response doesn't return the intended results if I pass cookies in the command line without using the cookie file. What important but subtle nuance am I overlooking here?