1libcurl(3) libcurl libcurl(3)
2
3
4
6 libcurl - client-side URL transfers
7
9 This is a short overview on how to use libcurl in your C programs.
10 There are specific man pages for each function mentioned in here. See
11 libcurl-easy(3), libcurl-multi(3), libcurl-share(3), libcurl-url(3),
12 libcurl-ws(3) and libcurl-tutorial(3) for in-depth understanding on how
13 to program with libcurl.
14
15 There are many bindings available that bring libcurl access to your fa‐
16 vorite language. Look elsewhere for documentation on those.
17
19 To transfer files, you create an "easy handle" using curl_easy_init(3)
20 for a single individual transfer (in either direction). You then set
21 your desired set of options in that handle with curl_easy_setopt(3).
22 Options you set with curl_easy_setopt(3) stick. They will be used on
23 every repeated use of this handle until you either change the option,
24 or you reset them all with curl_easy_reset(3).
25
26 To actually transfer data you have the option of using the "easy" in‐
27 terface, or the "multi" interface.
28
29 The easy interface is a synchronous interface with which you call
30 curl_easy_perform(3) and let it perform the transfer. When it is com‐
31 pleted, the function returns and you can continue. More details are
32 found in the libcurl-easy(3) man page.
33
34 The multi interface on the other hand is an asynchronous interface,
35 that you call and that performs only a little piece of the transfer on
36 each invoke. It is perfect if you want to do things while the transfer
37 is in progress, or similar. The multi interface allows you to select()
38 on libcurl action, and even to easily download multiple files simulta‐
39 neously using a single thread. See further details in the libcurl-
40 multi(3) man page.
41
42
44 There is also a series of other helpful functions and interface fami‐
45 lies to use, including these:
46
47 curl_version_info()
48 gets detailed libcurl (and other used libraries) version
49 info. See curl_version_info(3)
50
51 curl_getdate()
52 converts a date string to time_t. See curl_getdate(3)
53
54 curl_easy_getinfo()
55 get information about a performed transfer. See
56 curl_easy_getinfo(3)
57
58 curl_mime_addpart()
59 helps building an HTTP form POST. See curl_mime_ad‐
60 dpart(3)
61
62 curl_slist_append()
63 builds a linked list. See curl_slist_append(3)
64
65 Sharing
66 You can have multiple easy handles share certain data,
67 even if they are used in different threads. This magic is
68 setup using the share interface, as described in the
69 libcurl-share(3) man page.
70
71 URL Parsing
72 URL parsing and manipulations. See libcurl-url(3)
73
74 WebSocket communication
75 See libcurl-ws(3)
76
77
79 On unix-like machines, there's a tool named curl-config that gets in‐
80 stalled with the rest of the curl stuff when 'make install' is per‐
81 formed.
82
83 curl-config is added to make it easier for applications to link with
84 libcurl and developers to learn about libcurl and how to use it.
85
86 Run 'curl-config --libs' to get the (additional) linker options you
87 need to link with the particular version of libcurl you have installed.
88 See the curl-config(1) man page for further details.
89
90 Unix-like operating system that ship libcurl as part of their distribu‐
91 tions often do not provide the curl-config tool, but simply install the
92 library and headers in the common path for this purpose.
93
94 Many Linux and similar systems use pkg-config to provide build and link
95 options about libraries and libcurl supports that as well.
96
98 All public functions in the libcurl interface are prefixed with 'curl_'
99 (with a lowercase c). You can find other functions in the library
100 source code, but other prefixes indicate that the functions are private
101 and may change without further notice in the next release.
102
103 Only use documented functions and functionality!
104
106 libcurl works exactly the same, on any of the platforms it compiles and
107 builds on.
108
110 libcurl is thread safe but there are a few exceptions. Refer to
111 libcurl-thread(3) for more information.
112
113
115 Persistent connections means that libcurl can re-use the same connec‐
116 tion for several transfers, if the conditions are right.
117
118 libcurl will always attempt to use persistent connections. Whenever you
119 use curl_easy_perform(3) or curl_multi_perform(3) etc, libcurl will at‐
120 tempt to use an existing connection to do the transfer, and if none ex‐
121 ists it will open a new one that will be subject for re-use on a possi‐
122 ble following call to curl_easy_perform(3) or curl_multi_perform(3).
123
124 To allow libcurl to take full advantage of persistent connections, you
125 should do as many of your file transfers as possible using the same
126 handle.
127
128 If you use the easy interface, and you call curl_easy_cleanup(3), all
129 the possibly open connections held by libcurl will be closed and for‐
130 gotten.
131
132 When you have created a multi handle and are using the multi interface,
133 the connection pool is instead kept in the multi handle so closing and
134 creating new easy handles to do transfers will not affect them. Instead
135 all added easy handles can take advantage of the single shared pool.
136
138 There are a variety of constants that libcurl uses, mainly through its
139 internal use of other libraries, which are too complicated for the li‐
140 brary loader to set up. Therefore, a program must call a library func‐
141 tion after the program is loaded and running to finish setting up the
142 library code. For example, when libcurl is built for SSL capability via
143 the GNU TLS library, there is an elaborate tree inside that library
144 that describes the SSL protocol.
145
146 curl_global_init(3) is the function that you must call. This may allo‐
147 cate resources (e.g. the memory for the GNU TLS tree mentioned above),
148 so the companion function curl_global_cleanup(3) releases them.
149
150 If libcurl was compiled with support for multiple SSL backends, the
151 function curl_global_sslset(3) can be called before curl_global_init(3)
152 to select the active SSL backend.
153
154 The global constant functions are thread-safe since libcurl 7.84.0 if
155 curl_version_info(3) has the CURL_VERSION_THREADSAFE feature bit set
156 (most platforms). Read libcurl-thread(3) for thread safety guidelines.
157
158 If the global constant functions are not thread safe, then you must not
159 call them when any other thread in the program is running. It is not
160 good enough that no other thread is using libcurl at the time, because
161 these functions internally call similar functions of other libraries,
162 and those functions are similarly thread-unsafe. You cannot generally
163 know what these libraries are, or whether other threads are using them.
164
165 If the global constant functions are not thread safe, then the basic
166 rule for constructing a program that uses libcurl is this: Call
167 curl_global_init(3), with a CURL_GLOBAL_ALL argument, immediately after
168 the program starts, while it is still only one thread and before it
169 uses libcurl at all. Call curl_global_cleanup(3) immediately before the
170 program exits, when the program is again only one thread and after its
171 last use of libcurl.
172
173 It is not actually required that the functions be called at the begin‐
174 ning and end of the program -- that is just usually the easiest way to
175 do it.
176
177 You can call both of these multiple times, as long as all calls meet
178 these requirements and the number of calls to each is the same.
179
180 The global constant situation merits special consideration when the
181 code you are writing to use libcurl is not the main program, but rather
182 a modular piece of a program, e.g. another library. As a module, your
183 code does not know about other parts of the program -- it does not know
184 whether they use libcurl or not. And its code does not necessarily run
185 at the start and end of the whole program.
186
187 A module like this must have global constant functions of its own, just
188 like curl_global_init(3) and curl_global_cleanup(3). The module thus
189 has control at the beginning and end of the program and has a place to
190 call the libcurl functions. If multiple modules in the program use
191 libcurl, they all will separately call the libcurl functions, and that
192 is OK because only the first curl_global_init(3) and the last
193 curl_global_cleanup(3) in a program change anything. (libcurl uses a
194 reference count in static memory).
195
196 In a C++ module, it is common to deal with the global constant situa‐
197 tion by defining a special class that represents the global constant
198 environment of the module. A program always has exactly one object of
199 the class, in static storage. That way, the program automatically calls
200 the constructor of the object as the program starts up and the destruc‐
201 tor as it terminates. As the author of this libcurl-using module, you
202 can make the constructor call curl_global_init(3) and the destructor
203 call curl_global_cleanup(3) and satisfy libcurl's requirements without
204 your user having to think about it. (Caveat: If you are initializing
205 libcurl from a Windows DLL you should not initialize it from DllMain or
206 a static initializer because Windows holds the loader lock during that
207 time and it could cause a deadlock.)
208
209 curl_global_init(3) has an argument that tells what particular parts of
210 the global constant environment to set up. In order to successfully use
211 any value except CURL_GLOBAL_ALL (which says to set up the whole
212 thing), you must have specific knowledge of internal workings of
213 libcurl and all other parts of the program of which it is part.
214
215 A special part of the global constant environment is the identity of
216 the memory allocator. curl_global_init(3) selects the system default
217 memory allocator, but you can use curl_global_init_mem(3) to supply one
218 of your own. However, there is no way to use curl_global_init_mem(3) in
219 a modular program -- all modules in the program that might use libcurl
220 would have to agree on one allocator.
221
222 There is a failsafe in libcurl that makes it usable in simple situa‐
223 tions without you having to worry about the global constant environment
224 at all: curl_easy_init(3) sets up the environment itself if it has not
225 been done yet. The resources it acquires to do so get released by the
226 operating system automatically when the program exits.
227
228 This failsafe feature exists mainly for backward compatibility because
229 there was a time when the global functions did not exist. Because it is
230 sufficient only in the simplest of programs, it is not recommended for
231 any program to rely on it.
232
233
234
235libcurl 8.2.1 June 14, 2023 libcurl(3)