CURLOPT_COOKIEJAR(3)

πŸ’Ύ CURLOPT_COOKIEJAR(3) β€” the Library Functions Manual glow-up

okay bestie, welcome to CURLOPT_COOKIEJAR(3) from the Library Functions Manual ✨ this one is all about where your cookies go to live when the party's over, and honestly it's giving "I'm the cleanup crew, not the snack table" energy. let's get into it πŸͺ


✨ NAME ✨

CURLOPT_COOKIEJAR β€” the filename where your cookies get stored πŸ’–


πŸŽ€ SYNOPSIS πŸŽ€

#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_COOKIEJAR, char *filename);

🌸 DESCRIPTION 🌸

So you pass a filename as a char *, null-terminated, periodt. This is what makes libcurl write all internally known cookies to that file when curl_easy_cleanup(3) gets called. If there are no cookies being kept in memory at that moment, then bestie… no file gets created. It's giving "if you don't have anything nice to say, don't say anything at all" 🫢

Wanna write the cookies to stdout instead? Pass "-" as the filename and slay πŸ’…

Using this option also enables cookies for this session, so if you for example follow a redirect, it makes matching cookies get sent accordingly. That's the little bonus that comes free with the option, iykyk.

⚠️ Important vibe check: libcurl does not read any cookies from the cookie jar you specify with this option. It's write-only, like a notes app you never scroll up in. If you actually wanna read cookies from a file, use CURLOPT_COOKIEFILE(3).

And now for the cursed part 😭 if the cookie jar file cannot be created or written to (which happens when curl_easy_cleanup(3) is called), libcurl does not and cannot report an error for this. Like, it literally can't. Using CURLOPT_VERBOSE(3) or CURLOPT_DEBUGFUNCTION(3) displays a warning, but that is the only visible feedback you get about this possibly lethal situation. So like, lowkey pay attention or the cookies go down with the ship πŸ’€

Also, cookies that are imported in the Set-Cookie format without a domain name are not exported by this option. No domain, no passport, no export, sorry babe πŸ›‚

Good news though: the application does not have to keep the string around after setting this option. Set it and yeet it, the handle's got it from here.

And if you use this option multiple times? The last set string overrides the previous ones. Last one wins, main character energy only πŸ‘‘ Set it to NULL to disable its use again.


🚨 SECURITY CONCERNS 🚨

Okay this part is serious so stay with me πŸ™ˆ

libcurl cannot fully protect against attacks where an attacker has write access to the same directory where it is directed to save files. This is particularly sensitive if you save files using elevated privileges. Think about who can touch that folder, bestie, for real.

Also, libcurl creates the file to store cookies using default file permissions, meaning that on *nix systems you may need to restrict your umask to prevent other users on the same system from accessing the file. It's giving "shared apartment, private diary" β€” lock it down πŸ”’


πŸ’œ DEFAULT πŸ’œ

NULL

(iconic, minimal, does nothing until you tell it to)


🌈 PROTOCOLS 🌈

This functionality affects http only. Not https-adjacent-everything, not ftp, just http. Periodt.


🧚 EXAMPLE 🧚

okay here's the whole vibe in code, copy it exactly bestie:

int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    CURLcode result;
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin");

    /* export cookies to this file when closing the handle */
    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "/tmp/cookies.txt");

    result = curl_easy_perform(curl);

    /* close the handle, write the cookies */
    curl_easy_cleanup(curl);
  }
}

See how it's setting the jar before performing, and the cookies get written on curl_easy_cleanup? That's the whole flow. Obsessed πŸŽ€


⭐ AVAILABILITY ⭐

Added in curl 7.9. Vintage. A classic. We respect the elders πŸ’–


πŸ¦‹ RETURN VALUE πŸ¦‹

curl_easy_setopt(3) returns a CURLcode indicating success or error.

CURLE_OK (0) means everything was OK. Non-zero means an error occurred β€” see libcurl-errors(3) for the full tea β˜•


πŸ‘‘ SEE ALSO πŸ‘‘

go read those too, they're all in the cookie cinematic universe πŸͺ✨


okay that's the whole page bestie, no cap. this masterclass in cookie storage was brought to you by libcurl, dated 2026-10-12, on the CURLOPT_COOKIEJAR(3) page. now go forth and store your cookies responsibly πŸ’…πŸ«ΆπŸŒΈ

πŸ“¦ where this page comes from