CURLOPT_COOKIEFILE.md (2765B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_COOKIEFILE 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_COOKIE (3) 9 - CURLOPT_COOKIEJAR (3) 10 - CURLOPT_COOKIESESSION (3) 11 Protocol: 12 - HTTP 13 Added-in: 7.1 14 --- 15 16 # NAME 17 18 CURLOPT_COOKIEFILE - filename to read cookies from 19 20 # SYNOPSIS 21 22 ~~~c 23 #include <curl/curl.h> 24 25 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_COOKIEFILE, char *filename); 26 ~~~ 27 28 # DESCRIPTION 29 30 Pass a pointer to a null-terminated string as parameter. It should point to 31 the filename of your file holding cookie data to read. The cookie data can be 32 in either the old Netscape / Mozilla cookie data format or just regular HTTP 33 headers (Set-Cookie style) dumped to a file. 34 35 It also enables the cookie engine, making libcurl parse and send cookies on 36 subsequent requests with this handle. 37 38 By passing the empty string ("") to this option, you enable the cookie engine 39 without reading any initial cookies. If you tell libcurl the filename is "-" 40 (just a single minus sign), libcurl instead reads from stdin. 41 42 This option only **reads** cookies. To make libcurl write cookies to file, 43 see CURLOPT_COOKIEJAR(3). 44 45 If you read cookies from a plain HTTP headers file and it does not specify a 46 domain in the Set-Cookie line, then the cookie is not sent since the cookie 47 domain cannot match the target URL's. To address this, set a domain in 48 Set-Cookie line (doing that includes subdomains) or preferably: use the 49 Netscape format. 50 51 The application does not have to keep the string around after setting this 52 option. 53 54 If you use this option multiple times, you add more files to read cookies 55 from. Setting this option to NULL disables the cookie engine and clears the 56 list of files to read cookies from. 57 58 # SECURITY CONCERNS 59 60 This document previously mentioned how specifying a non-existing file can also 61 enable the cookie engine. While true, we strongly advise against using that 62 method as it is too hard to be sure that files that stay that way in the long 63 run. 64 65 # DEFAULT 66 67 NULL 68 69 # %PROTOCOLS% 70 71 # EXAMPLE 72 73 ~~~c 74 int main(void) 75 { 76 CURL *curl = curl_easy_init(); 77 if(curl) { 78 CURLcode res; 79 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/foo.bin"); 80 81 /* get cookies from an existing file */ 82 curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "/tmp/cookies.txt"); 83 84 res = curl_easy_perform(curl); 85 86 curl_easy_cleanup(curl); 87 } 88 } 89 ~~~ 90 91 # Cookie file format 92 93 The cookie file format and general cookie concepts in curl are described 94 online here: https://curl.se/docs/http-cookies.html 95 96 # %AVAILABILITY% 97 98 # RETURN VALUE 99 100 curl_easy_setopt(3) returns a CURLcode indicating success or error. 101 102 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 103 libcurl-errors(3).