Sometimes when using external APIs it’s necessary to store returned cookies, for example if a cookie is being used for authentication.
There are two sides to this coin, saving the cookie, and then sending it again on subsequent requests. cURL offers options for this, --cookie-jar
(or -c
) to save cookies, and --cookie
(or -b
) to send cookies back again.
Let’s imagine that you’re performing authentication by posting data to an API. Your cURL options might look something like this, assuming that the data to be sent is stored in the local variable $data.
You’ll see that the last options being set is --cookie
, and the returned cookie data is being saved into global variable $$CookieJar
.
Here’s an example of what we end up with in that global variable
You can see that a lot of it’s ‘header’ information, but then the last line is an actual cookie value which has been returned. It amuses me that in 2017 the format of the file still confirms to a Netscape specification from 1994 (hands up if you’re old enough to remember Netscape Navigator)
As an aside, the values in that row are tab separated, and relate to
- the server the cookie is from (api.msdev.co.uk)
- can sub-domains of the domain access the cookie (FALSE)
- the path on the server the cookie is valid for (‘/’ i.e. everywhere)
- is https required to use this cookie (FALSE)
- when the cookie expires (0 means ‘at the end of the session’, any other number is a Unix timestamp)
- the name of the cookie (PHPSESSID)
- the cookie value (04oj6mj6hglq0t8iibkmql0u71)
All good! Now when we want to make a subsequent call to that server we need to send the content of the $$CookieJar back again. Unfortunately it’s not quite as simple as it might seem – we need to do a little manipulation of that variable – like this:
What we’re doing here is replacing the paragraph breaks in the global variable with a ‘line feed’ character – this allows the resulting variable to sent as a string. For our subsequent call, we then add that to the cURL options:
It’s important to note that you need to place the variable name inside the double quotes as in the example above – if you think you can be clever and skip using the local variable and just do
then be prepared for disappointment and frustration!
If you’re going to be at DevCon this year, then come and see my Explore cURL for FileMaker session on Tuesday at 1415 where I’ll remind you of this important ‘gotcha’ and show you lots more magic that we now have access to through cURL options.
Thank you for your writing about “The Cookie Jar”. It’s help my work is done.
September 24, 2021 at 9:08 amThank you very much.