xref: /curl/docs/libcurl/opts/CURLOPT_COOKIEJAR.md (revision e3fe0200)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_COOKIEJAR
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_COOKIE (3)
9  - CURLOPT_COOKIEFILE (3)
10  - CURLOPT_COOKIELIST (3)
11Protocol:
12  - HTTP
13---
14
15# NAME
16
17CURLOPT_COOKIEJAR - filename to store cookies to
18
19# SYNOPSIS
20
21~~~c
22#include <curl/curl.h>
23
24CURLcode curl_easy_setopt(CURL *handle, CURLOPT_COOKIEJAR, char *filename);
25~~~
26
27# DESCRIPTION
28
29Pass a *filename* as a char *, null-terminated. This makes libcurl write
30all internally known cookies to the specified file when
31curl_easy_cleanup(3) is called. If no cookies are kept in memory at that
32time, no file is created. Specify "-" as filename to instead have the cookies
33written to stdout. Using this option also enables cookies for this session, so
34if you for example follow a redirect it makes matching cookies get sent
35accordingly.
36
37Note that libcurl does not read any cookies from the cookie jar specified with
38this option. To read cookies from a file, use CURLOPT_COOKIEFILE(3).
39
40If the cookie jar file cannot be created or written to (when the
41curl_easy_cleanup(3) is called), libcurl does not and cannot report an
42error for this. Using CURLOPT_VERBOSE(3) or
43CURLOPT_DEBUGFUNCTION(3) displays a warning, but that is the only
44visible feedback you get about this possibly lethal situation.
45
46Cookies are imported in the Set-Cookie format without a domain name are not
47exported by this option.
48
49The application does not have to keep the string around after setting this
50option.
51
52# DEFAULT
53
54NULL
55
56# EXAMPLE
57
58~~~c
59int main(void)
60{
61  CURL *curl = curl_easy_init();
62  if(curl) {
63    CURLcode res;
64    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin");
65
66    /* export cookies to this file when closing the handle */
67    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "/tmp/cookies.txt");
68
69    res = curl_easy_perform(curl);
70
71    /* close the handle, write the cookies! */
72    curl_easy_cleanup(curl);
73  }
74}
75~~~
76
77# AVAILABILITY
78
79Along with HTTP
80
81# RETURN VALUE
82
83Returns CURLE_OK if HTTP is supported, CURLE_UNKNOWN_OPTION if not, or
84CURLE_OUT_OF_MEMORY if there was insufficient heap space.
85