xref: /curl/docs/libcurl/opts/CURLOPT_COOKIEJAR.md (revision 3040971d)
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
13Added-in: 7.9
14---
15
16# NAME
17
18CURLOPT_COOKIEJAR - filename to store cookies to
19
20# SYNOPSIS
21
22~~~c
23#include <curl/curl.h>
24
25CURLcode curl_easy_setopt(CURL *handle, CURLOPT_COOKIEJAR, char *filename);
26~~~
27
28# DESCRIPTION
29
30Pass a *filename* as a char *, null-terminated. This makes libcurl write all
31internally known cookies to the specified file when curl_easy_cleanup(3) is
32called. If no cookies are kept in memory at that time, no file is created.
33Specify "-" as filename to instead have the cookies written to stdout. Using
34this option also enables cookies for this session, so if you for example
35follow a redirect it makes matching cookies get sent accordingly.
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 error
42for this. Using CURLOPT_VERBOSE(3) or CURLOPT_DEBUGFUNCTION(3) displays a
43warning, but that is the only visible feedback you get about this possibly
44lethal 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
52Using this option multiple times makes the last set string override the
53previous ones. Set it to NULL to disable its use again.
54
55# DEFAULT
56
57NULL
58
59# %PROTOCOLS%
60
61# EXAMPLE
62
63~~~c
64int main(void)
65{
66  CURL *curl = curl_easy_init();
67  if(curl) {
68    CURLcode res;
69    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin");
70
71    /* export cookies to this file when closing the handle */
72    curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "/tmp/cookies.txt");
73
74    res = curl_easy_perform(curl);
75
76    /* close the handle, write the cookies */
77    curl_easy_cleanup(curl);
78  }
79}
80~~~
81
82# %AVAILABILITY%
83
84# RETURN VALUE
85
86Returns CURLE_OK if HTTP is supported, CURLE_UNKNOWN_OPTION if not, or
87CURLE_OUT_OF_MEMORY if there was insufficient heap space.
88