CURLOPT_SHARE(3)

πŸ’… CURLOPT_SHARE β€” share handle to use ✨

Okay so bestie, this is the CURLOPT_SHARE page from the Library Functions Manual, and it's genuinely one of my faves because it's all about sharing and that's the whole vibe. 🫢 Let me break it down for you.

✨ SYNOPSIS ✨

#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_SHARE, CURLSH *share);

🌸 DESCRIPTION 🌸

So basically, you pass a share handle as a parameter. That share handle must have been created by a previous call to curl_share_init(3) β€” you can't just invent one, okay? It has to be the real thing. πŸ’–

When you set this option, it makes this curl handle use the data from the shared handle instead of keeping the data to itself. And like β€” that's the whole point! This is what enables several curl handles to share data together. Teamwork, community, found family. 😭

⚠️ Now listen, this part is important: if the curl handles are used simultaneously in multiple threads, you must use the locking methods in the share handle. Go check curl_share_setopt(3) for the details β€” do not skip the locks, I'm serious.

Cookie tea πŸͺ: if you add a share that is set to share cookies, your easy handle uses that cookie cache and gets the cookie engine enabled. But if you stop sharing an object that was using cookies β€” or you change to another object that does not share cookies β€” the easy handle gets its cookie engine disabled. It's giving "you're not in the group chat anymore" but for cookies, ngl.

Data that the share object is not set to share? Dealt with the usual way, as if no share was used. So no weird side effects, no drama, we love that for you. ✨

And when you're done sharing, set this option to NULL again to stop using that share object. Clean exit, periodt. πŸ’…

πŸŽ€ DEFAULT πŸŽ€

NULL β€” no share, you're on your own, solo main character era.

πŸ¦‹ PROTOCOLS πŸ¦‹

This functionality affects all supported protocols. Every single one, no favorites. 🌈

πŸ”₯ EXAMPLE πŸ”₯

Okay here's the whole thing β€” curl handle number one and curl handle number two sharing cookies like the besties they are:

int main(void)
{
  CURL *curl = curl_easy_init();
  CURL *curl2 = curl_easy_init(); /* a second handle */
  if(curl) {
    CURLcode result;
    CURLSH *shobject = curl_share_init();
    curl_share_setopt(shobject, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);

    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
    curl_easy_setopt(curl, CURLOPT_SHARE, shobject);
    result = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    /* the second handle shares cookies from the first */
    curl_easy_setopt(curl2, CURLOPT_URL, "https://example.com/second");
    curl_easy_setopt(curl2, CURLOPT_COOKIEFILE, "");
    curl_easy_setopt(curl2, CURLOPT_SHARE, shobject);
    result = curl_easy_perform(curl2);
    curl_easy_cleanup(curl2);

    curl_share_cleanup(shobject);
  }
}

πŸ’– AVAILABILITY πŸ’–

Added in curl 7.10 β€” vintage but still slaying, iykyk.

🫢 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 rundown.

πŸ’œ SEE ALSO πŸ’œ


And that's the whole page, bestie! πŸŽ€ Signing off on behalf of libcurl, dated 2026-10-12 β€” go forth and share responsibly. πŸ’–πŸ”₯

πŸ“¦ where this page comes from