curl_easy_setopt (3) Linux Manual Page
NAME
curl_easy_setopt – set options for a curl easy handle
SYNOPSIS
#include <curl/curl.h>
CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
DESCRIPTION
curl_easy_setopt() is used to tell libcurl how to behave. By using the appropriate options to curl_easy_setopt, you can change libcurl’s behavior. All options are set with the option followed by a parameter. That parameter can be a long, a function pointer, an object pointer or a curl_off_t, depending on what the specific option expects. Read this manual carefully as bad input values may cause libcurl to behave badly! You can only set one option in each function call. A typical application uses many curl_easy_setopt() calls in the setup phase.
Options set with this function call are valid for all forthcoming transfers performed using this handle. The options are not in any way reset between transfers, so if you want subsequent transfers with different options, you must change them between the transfers. You can optionally reset all options back to internal default with curl_easy_reset(3).
Strings passed to libcurl as ‘char *’ arguments, are copied by the library; thus the string storage associated to the pointer argument may be overwritten after curl_easy_setopt() returns. Exceptions to this rule are described in the option details below.
Before version 7.17.0, strings were not copied. Instead the user was forced keep them available until libcurl no longer needed them.
The handle is the return code from a curl_easy_init(3) or curl_easy_duphandle(3) call.
BEHAVIOR OPTIONS
- CURLOPT_VERBOSE
- Set the parameter to 1 to get the library to display a lot of verbose information about its operations. Very useful for libcurl and/or protocol debugging and understanding. The verbose information will be sent to stderr, or the stream set with CURLOPT_STDERR.
You hardly ever want this set in production use, you will almost always want this when you debug/report problems. Another neat option for debugging is the CURLOPT_DEBUGFUNCTION.
- CURLOPT_HEADER
- A parameter set to 1 tells the library to include the header in the body output. This is only relevant for protocols that actually have headers preceding the data (like HTTP).
- CURLOPT_NOPROGRESS
- Pass a long. If set to 1, it tells the library to shut off the progress meter completely. It will also prevent the CURLOPT_PROGRESSFUNCTION from getting called.
Future versions of libcurl are likely to not have any built-in progress meter at all.
- CURLOPT_NOSIGNAL
- Pass a long. If it is 1, libcurl will not use any functions that install signal handlers or any functions that cause signals to be sent to the process. This option is mainly here to allow multi-threaded unix applications to still set/use all timeout options etc, without risking getting signals. (Added in 7.10)
If this option is set and libcurl has been built with the standard name resolver, timeouts will not occur while the name resolve takes place. Consider building libcurl with c-ares support to enable asynchronous DNS lookups, which enables nice timeouts for name resolves without signals.
Setting CURLOPT_NOSIGNAL to 1 makes libcurl NOT ask the system to ignore SIGPIPE signals, which otherwise are sent by the system when trying to send data to a socket which is closed in the other end. libcurl makes an effort to never cause such SIGPIPEs to trigger, but some operating systems have no way to avoid them and even on those that have there are some corner cases when they may still happen, contrary to our desire. In addition, using CURLAUTH_NTLM_WB authentication could cause a SIGCHLD signal to be raised.
- CURLOPT_WILDCARDMATCH
- Set this option to 1 if you want to transfer multiple files according to a file name pattern. The pattern can be specified as part of the CURLOPT_URL option, using an fnmatch-like pattern (Shell Pattern Matching) in the last part of URL (file name).
By default, libcurl uses its internal wildcard matching implementation. You can provide your own matching function by the CURLOPT_FNMATCH_FUNCTION option.
This feature is only supported by the FTP download for now.
A brief introduction of its syntax follows:
-
- * – ASTERISK
- ftp://example.com/some/path/
*.txt(for all txt’s from the root directory)
-
- ? – QUESTION MARK
- Question mark matches any (exactly one) character.
ftp://example.com/some/path/
photo?.jpeg
-
- [ – BRACKET EXPRESSION
- The left bracket opens a bracket expression. The question mark and asterisk have no special meaning in a bracket expression. Each bracket expression ends by the right bracket and matches exactly one character. Some examples follow:
[a-zA-Z0-9]or[f-gF-G]– character interval[abc]– character enumeration[^abc]or[!abc]– negation[[:name:]]class expression. Supported classes arealnum,lower,space,alpha,digit,print,upper,blank,graph,xdigit.[][-!^]– special case – matches only ‘-‘, ‘]’, ‘[‘, ‘!’ or ‘^’. These characters have no special purpose.[\[\]\]– escape syntax. Matches ‘[‘, ‘]’ or ‘\’.Using the rules above, a file name pattern can be constructed:
ftp://example.com/some/path/
[a-z[:upper:]\].jpeg
(This was added in 7.21.0)
CALLBACK OPTIONS
- CURLOPT_WRITEFUNCTION
- Pass a pointer to a function that matches the following prototype:
size_t function( char *ptr, size_t size, size_t nmemb, void *userdata);This function gets called by libcurl as soon as there is data received that needs to be saved. The size of the data pointed to by ptr is size multiplied with nmemb, it will not be zero terminated. Return the number of bytes actually taken care of. If that amount differs from the amount passed to your function, it’ll signal an error to the library. This will abort the transfer and return CURLE_WRITE_ERROR.From 7.18.0, the function can return CURL_WRITEFUNC_PAUSE which then will cause writing to this connection to become paused. See curl_easy_pause(3) for further details.
This function may be called with zero bytes data if the transferred file is empty.
Set this option to NULL to get the internal default function. The internal default function will write the data to the FILE * given with CURLOPT_WRITEDATA.
Set the userdata argument with the CURLOPT_WRITEDATA option.
The callback function will be passed as much data as possible in all invokes, but you cannot possibly make any assumptions. It may be one byte, it may be thousands. The maximum amount of body data that can be passed to the write callback is defined in the curl.h header file: CURL_MAX_WRITE_SIZE (the usual default is 16K). If you however have CURLOPT_HEADER set, which sends header data to the write callback, you can get up to CURL_MAX_HTTP_HEADER bytes of header data passed into it. This usually means 100K.
- CURLOPT_WRITEDATA
- Data pointer to pass to the file write function. If you use the CURLOPT_WRITEFUNCTION option, this is the pointer you’ll get as input. If you don’t use a callback, you must pass a ‘FILE *’ as libcurl will pass this to fwrite() when writing data.
The internal CURLOPT_WRITEFUNCTION will write the data to the FILE * given with this option, or to stdout if this option hasn’t been set.
If you’re using libcurl as a win32 DLL, you
MUSTuse the CURLOPT_WRITEFUNCTION if you set this option or you will experience crashes.This option is also known with the older name CURLOPT_FILE, the name CURLOPT_WRITEDATA was introduced in 7.9.7.
- CURLOPT_READFUNCTION
- Pass a pointer to a function that matches the following prototype:
size_t function( void *ptr, size_t size, size_t nmemb, void *userdata);This function gets called by libcurl as soon as it needs to read data in order to send it to the peer. The data area pointed at by the pointer ptr may be filled with at most size multiplied with nmemb number of bytes. Your function must return the actual number of bytes that you stored in that memory area. Returning 0 will signal end-of-file to the library and cause it to stop the current transfer.If you stop the current transfer by returning 0 "pre-maturely" (i.e before the server expected it, like when you’ve said you will upload N bytes and you upload less than N bytes), you may experience that the server "hangs" waiting for the rest of the data that won’t come.
The read callback may return CURL_READFUNC_ABORT to stop the current operation immediately, resulting in a CURLE_ABORTED_BY_CALLBACK error code from the transfer (Added in 7.12.1)
From 7.18.0, the function can return CURL_READFUNC_PAUSE which then will cause reading from this connection to become paused. See curl_easy_pause(3) for further details.
Bugs: when doing TFTP uploads, you must return the exact amount of data that the callback wants, or it will be considered the final packet by the server end and the transfer will end there.If you set this callback pointer to NULL, or don’t set it at all, the default internal read function will be used. It is doing an fread() on the FILE * userdata set with CURLOPT_READDATA.
- CURLOPT_READDATA
- Data pointer to pass to the file read function. If you use the CURLOPT_READFUNCTION option, this is the pointer you’ll get as input. If you don’t specify a read callback but instead rely on the default internal read function, this data must be a valid readable FILE *.
If you’re using libcurl as a win32 DLL, you MUST use a CURLOPT_READFUNCTION if you set this option.
This option was also known by the older name CURLOPT_INFILE, the name CURLOPT_READDATA was introduced in 7.9.7.
- CURLOPT_IOCTLFUNCTION
- Pass a pointer to a function that matches the following prototype:
curlioerr function(CURL *handle, int cmd, void *clientp);. This function gets called by libcurl when something special I/O-related needs to be done that the library can’t do by itself. For now, rewinding the read data stream is the only action it can request. The rewinding of the read data stream may be necessary when doing a HTTP PUT or POST with a multi-pass authentication method. (Option added in 7.12.3).Use CURLOPT_SEEKFUNCTION instead to provide seeking!
- CURLOPT_IOCTLDATA
- Pass a pointer that will be untouched by libcurl and passed as the 3rd argument in the ioctl callback set with CURLOPT_IOCTLFUNCTION. (Option added in 7.12.3)
- CURLOPT_SEEKFUNCTION
- Pass a pointer to a function that matches the following prototype:
int function(void *instream, curl_off_t offset, int origin);This function gets called by libcurl to seek to a certain position in the input stream and can be used to fast forward a file in a resumed upload (instead of reading all uploaded bytes with the normal read function/callback). It is also called to rewind a stream when doing a HTTP PUT or POST with a multi-pass authentication method. The function shall work like "fseek" or "lseek" and accepted SEEK_SET, SEEK_CUR and SEEK_END as argument for origin, although (in 7.18.0) libcurl only passes SEEK_SET. The callback must return 0 (CURL_SEEKFUNC_OK) on success, 1 (CURL_SEEKFUNC_FAIL) to cause the upload operation to fail or 2 (CURL_SEEKFUNC_CANTSEEK) to indicate that while the seek failed, libcurl is free to work around the problem if possible. The latter can sometimes be done by instead reading from the input or similar.If you forward the input arguments directly to "fseek" or "lseek", note that the data type for offset is not the same as defined for curl_off_t on many systems! (Option added in 7.18.0)
- CURLOPT_SEEKDATA
- Data pointer to pass to the file seek function. If you use the CURLOPT_SEEKFUNCTION option, this is the pointer you’ll get as input. If you don’t specify a seek callback, NULL is passed. (Option added in 7.18.0)
- CURLOPT_SOCKOPTFUNCTION
- Pass a pointer to a function that matches the following prototype:
int function(void *clientp, curl_socket_t curlfd, curlsocktype purpose);. This function gets called by libcurl after the socket() call but before the connect() call. The callback’s purpose argument identifies the exact purpose for this particular socket:CURLSOCKTYPE_IPCXN for actively created connections or since 7.28.0 CURLSOCKTYPE_ACCEPT for FTP when the connection was setup with PORT/EPSV (in earlier versions these sockets weren’t passed to this callback).
Future versions of libcurl may support more purposes. It passes the newly created socket descriptor so additional setsockopt() calls can be done at the user’s discretion. Return 0 (zero) from the callback on success. Return 1 from the callback function to signal an unrecoverable error to the library and it will close the socket and return CURLE_COULDNT_CONNECT. (Option added in 7.16.0)
Added in 7.21.5, the callback function may return CURL_SOCKOPT_ALREADY_CONNECTED, which tells libcurl that the socket is in fact already connected and then libcurl will not attempt to connect it.
- CURLOPT_SOCKOPTDATA
- Pass a pointer that will be untouched by libcurl and passed as the first argument in the sockopt callback set with CURLOPT_SOCKOPTFUNCTION. (Option added in 7.16.0)
- CURLOPT_OPENSOCKETFUNCTION
- Pass a pointer to a function that matches the following prototype:
curl_socket_t function(void *clientp, curlsocktype purpose, struct curl_sockaddr *address);. This function gets called by libcurl instead of the socket(2) call. The callback’s purpose argument identifies the exact purpose for this particular socket: CURLSOCKTYPE_IPCXN is for IP based connections. Future versions of libcurl may support more purposes. It passes the resolved peer address as a address argument so the callback can modify the address or refuse to connect at all. The callback function should return the socket or CURL_SOCKET_BAD in case no connection could be established or another error was detected. Any additional setsockopt(2) calls can be done on the socket at the user’s discretion. CURL_SOCKET_BAD return value from the callback function will signal an unrecoverable error to the library and it will return CURLE_COULDNT_CONNECT. This return code can be used for IP address blacklisting. The default behavior is:return socket(addr->family, addr->socktype, addr->protocol);(Option added in 7.17.1.)
- CURLOPT_OPENSOCKETDATA
- Pass a pointer that will be untouched by libcurl and passed as the first argument in the opensocket callback set with CURLOPT_OPENSOCKETFUNCTION. (Option added in 7.17.1.)
- CURLOPT_CLOSESOCKETFUNCTION
- Pass a pointer to a function that matches the following prototype:
int function(void *clientp, curl_socket_t item);. This function gets called by libcurl instead of the close(3) or closesocket(3) call when sockets are closed (not for any other file descriptors). This is pretty much the reverse to the CURLOPT_OPENSOCKETFUNCTION option. Return 0 to signal success and 1 if there was an error. (Option added in 7.21.7) - CURLOPT_CLOSESOCKETDATA
- Pass a pointer that will be untouched by libcurl and passed as the first argument in the closesocket callback set with CURLOPT_CLOSESOCKETFUNCTION. (Option added in 7.21.7)
- CURLOPT_PROGRESSFUNCTION
- Pass a pointer to a function that matches the following prototype:
int function(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);. This function gets called by libcurl instead of its internal equivalent with a frequent interval during operation (roughly once per second or sooner) no matter if data is being transferred or not. Unknown/unused argument values passed to the callback will be set to zero (like if you only download data, the upload size will remain 0). Returning a non-zero value from this callback will cause libcurl to abort the transfer and return CURLE_ABORTED_BY_CALLBACK.If you transfer data with the multi interface, this function will not be called during periods of idleness unless you call the appropriate libcurl function that performs transfers.
CURLOPT_NOPROGRESS must be set to 0 to make this function actually get called.
- CURLOPT_PROGRESSDATA
- Pass a pointer that will be untouched by libcurl and passed as the first argument in the progress callback set with CURLOPT_PROGRESSFUNCTION.
- CURLOPT_HEADERFUNCTION
- Pass a pointer to a function that matches the following prototype:
size_t function( void *ptr, size_t size, size_t nmemb, void *userdata);. This function gets called by libcurl as soon as it has received header data. The header callback will be called once for each header and only complete header lines are passed on to the callback. Parsing headers is very easy using this. The size of the data pointed to by ptr is size multiplied with nmemb. Do not assume that the header line is zero terminated! The pointer named userdata is the one you set with the CURLOPT_WRITEHEADER option. The callback function must return the number of bytes actually taken care of. If that amount differs from the amount passed to your function, it’ll signal an error to the library. This will abort the transfer and return CURL_WRITE_ERROR.A complete HTTP header that is passed to this function can be up to CURL_MAX_HTTP_HEADER (100K) bytes.
If this option is not set, or if it is set to NULL, but CURLOPT_HEADERDATA (CURLOPT_WRITEHEADER) is set to anything but NULL, the function used to accept response data will be used instead. That is, it will be the function specified with CURLOPT_WRITEFUNCTION, or if it is not specified or NULL – the default, stream-writing function.
It’s important to note that the callback will be invoked for the headers of all responses received after initiating a request and not just the final response. This includes all responses which occur during authentication negotiation. If you need to operate on only the headers from the final response, you will need to collect headers in the callback yourself and use HTTP status lines, for example, to delimit response boundaries.
When a server sends a chunked encoded transfer, it may contain a trailer. That trailer is identical to a HTTP header and if such a trailer is received it is passed to the application using this callback as well. There are several ways to detect it being a trailer and not an ordinary header: 1) it comes after the response-body. 2) it comes after the final header line (CR LF) 3) a Trailer: header among the regular response-headers mention what header(s) to expect in the trailer.
For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function will get called with the server responses to the commands that libcurl sends.
- CURLOPT_WRITEHEADER
- (This option is also known as
CURLOPT_HEADERDATA) Pass a pointer to be used to write the header part of the received data to. If you don’t use CURLOPT_WRITEFUNCTION or CURLOPT_HEADERFUNCTION to take care of the writing, this must be a valid FILE * as the internal default will then be a plain fwrite(). See also the CURLOPT_HEADERFUNCTION option above on how to set a custom get-all-headers callback. - CURLOPT_DEBUGFUNCTION
- Pass a pointer to a function that matches the following prototype:
int curl_debug_callback (CURL *, curl_infotype, char *, size_t, void *);CURLOPT_DEBUGFUNCTION replaces the standard debug function used when CURLOPT_VERBOSE is in effect. This callback receives debug information, as specified with thecurl_infotypeargument. This function must return 0. The data pointed to by the char * passed to this function WILL NOT be zero terminated, but will be exactly of the size as told by the size_t argument.Available curl_infotype values:
-
- CURLINFO_TEXT
- The data is informational text.
- CURLINFO_HEADER_IN
- The data is header (or header-like) data received from the peer.
- CURLINFO_HEADER_OUT
- The data is header (or header-like) data sent to the peer.
- CURLINFO_DATA_IN
- The data is protocol data received from the peer.
- CURLINFO_DATA_OUT
- The data is protocol data sent to the peer.
- CURLOPT_DEBUGDATA
- Pass a pointer to whatever you want passed in to your CURLOPT_DEBUGFUNCTION in the last void * argument. This pointer is not used by libcurl, it is only passed to the callback.
- CURLOPT_SSL_CTX_FUNCTION
- This option does only function for libcurl powered by OpenSSL. If libcurl was built against another SSL library, this functionality is absent.
Pass a pointer to a function that matches the following prototype:
CURLcode sslctxfun(CURL *curl, void *sslctx, void *parm);This function gets called by libcurl just before the initialization of a SSL connection after having processed all other SSL related options to give a last chance to an application to modify the behaviour of openssl’s ssl initialization. The sslctx parameter is actually a pointer to an openssl SSL_CTX. If an error is returned no attempt to establish a connection is made and the perform operation will return the error code from this callback function. Set the parm argument with the CURLOPT_SSL_CTX_DATA option. This option was introduced in 7.11.0.This function will get called on all new connections made to a server, during the SSL negotiation. The SSL_CTX pointer will be a new one every time.
To use this properly, a non-trivial amount of knowledge of the openssl libraries is necessary. For example, using this function allows you to use openssl callbacks to add additional validation code for certificates, and even to change the actual URI of a HTTPS request (example used in the lib509 test case). See also the example section for a replacement of the key, certificate and trust file settings.
- CURLOPT_SSL_CTX_DATA
- Data pointer to pass to the ssl context callback set by the option CURLOPT_SSL_CTX_FUNCTION, this is the pointer you’ll get as third parameter, otherwise
NULL. (Added in 7.11.0) - CURLOPT_CONV_TO_NETWORK_FUNCTION
- CURLOPT_CONV_FROM_NETWORK_FUNCTION
- CURLOPT_CONV_FROM_UTF8_FUNCTION
- Pass a pointer to a function that matches the following prototype:
CURLcode function(char *ptr, size_t length);These three options apply to non-ASCII platforms only. They are available only if
CURL_DOES_CONVERSIONSwas defined when libcurl was built. When this is the case, curl_version_info(3) will return the CURL_VERSION_CONV feature bit set.The data to be converted is in a buffer pointed to by the ptr parameter. The amount of data to convert is indicated by the length parameter. The converted data overlays the input data in the buffer pointed to by the ptr parameter. CURLE_OK should be returned upon successful conversion. A CURLcode return value defined by curl.h, such as CURLE_CONV_FAILED, should be returned if an error was encountered.
CURLOPT_CONV_TO_NETWORK_FUNCTIONandCURLOPT_CONV_FROM_NETWORK_FUNCTIONconvert between the host encoding and the network encoding. They are used when commands or ASCII data are sent/received over the network.CURLOPT_CONV_FROM_UTF8_FUNCTIONis called to convert from UTF8 into the host encoding. It is required only for SSL processing.If you set a callback pointer to NULL, or don’t set it at all, the built-in libcurl iconv functions will be used. If HAVE_ICONV was not defined when libcurl was built, and no callback has been established, conversion will return the CURLE_CONV_REQD error code.
If HAVE_ICONV is defined, CURL_ICONV_CODESET_OF_HOST must also be defined. For example:
#define CURL_ICONV_CODESET_OF_HOST "IBM-1047" The iconv code in libcurl will default the network and UTF8 codeset names as follows:
#define CURL_ICONV_CODESET_OF_NETWORK "ISO8859-1" #define CURL_ICONV_CODESET_FOR_UTF8 "UTF-8" You will need to override these definitions if they are different on your system.
- CURLOPT_INTERLEAVEFUNCTION
- Pass a pointer to a function that matches the following prototype:
size_t function( void *ptr, size_t size, size_t nmemb, void *userdata). This function gets called by libcurl as soon as it has received interleaved RTP data. This function gets called for each $ block and therefore contains exactly one upper-layer protocol unit (e.g. one RTP packet). Curl writes the interleaved header as well as the included data for each call. The first byte is always an ASCII dollar sign. The dollar sign is followed by a one byte channel identifier and then a 2 byte integer length in network byte order. See RFC2326 Section 10.12 for more information on how RTP interleaving behaves. If unset or set to NULL, curl will use the default write function.Interleaved RTP poses some challenges for the client application. Since the stream data is sharing the RTSP control connection, it is critical to service the RTP in a timely fashion. If the RTP data is not handled quickly, subsequent response processing may become unreasonably delayed and the connection may close. The application may use CURL_RTSPREQ_RECEIVE to service RTP data when no requests are desired. If the application makes a request, (e.g. CURL_RTSPREQ_PAUSE) then the response handler will process any pending RTP data before marking the request as finished. (Added in 7.20.0)
- CURLOPT_INTERLEAVEDATA
- This is the userdata pointer that will be passed to CURLOPT_INTERLEAVEFUNCTION when interleaved RTP data is received. (Added in 7.20.0)
- CURLOPT_CHUNK_BGN_FUNCTION
- Pass a pointer to a function that matches the following prototype:
long function (const void *transfer_info, void *ptr, int remains). This function gets called by libcurl before a part of the stream is going to be transferred (if the transfer supports chunks).This callback makes sense only when using the CURLOPT_WILDCARDMATCH option for now.
The target of transfer_info parameter is a "feature depended" structure. For the FTP wildcard download, the target is curl_fileinfo structure (see curl/curl.h). The parameter ptr is a pointer given by CURLOPT_CHUNK_DATA. The parameter remains contains number of chunks remaining per the transfer. If the feature is not available, the parameter has zero value.
Return CURL_CHUNK_BGN_FUNC_OK if everything is fine, CURL_CHUNK_BGN_FUNC_SKIP if you want to skip the concrete chunk or CURL_CHUNK_BGN_FUNC_FAIL to tell libcurl to stop if some error occurred. (This was added in 7.21.0)
- CURLOPT_CHUNK_END_FUNCTION
- Pass a pointer to a function that matches the following prototype:
long function(void *ptr). This function gets called by libcurl as soon as a part of the stream has been transferred (or skipped).Return CURL_CHUNK_END_FUNC_OK if everything is fine or
CURL_CHUNK_END_FUNC_FAILto tell the lib to stop if some error occurred. (This was added in 7.21.0) - CURLOPT_CHUNK_DATA
- Pass a pointer that will be untouched by libcurl and passed as the ptr argument to the CURL_CHUNK_BGN_FUNTION and CURL_CHUNK_END_FUNTION. (This was added in 7.21.0)
- CURLOPT_FNMATCH_FUNCTION
- Pass a pointer to a function that matches the following prototype:
int function(void *ptr, const char *pattern, const char *string)prototype (see curl/curl.h). It is used internally for the wildcard matching feature.Return CURL_FNMATCHFUNC_MATCH if pattern matches the string, CURL_FNMATCHFUNC_NOMATCH if not or CURL_FNMATCHFUNC_FAIL if an error occurred. (This was added in 7.21.0)
- CURLOPT_FNMATCH_DATA
- Pass a pointer that will be untouched by libcurl and passed as the ptr argument to the CURL_FNMATCH_FUNCTION. (This was added in 7.21.0)
ERROR OPTIONS
- CURLOPT_ERRORBUFFER
- Pass a char * to a buffer that the libcurl may store human readable error messages in. This may be more helpful than just the return code from curl_easy_perform. The buffer must be at least CURL_ERROR_SIZE big. Although this argument is a ‘char *’, it does not describe an input string. Therefore the (probably undefined) contents of the buffer is NOT copied by the library. You must keep the associated storage available until libcurl no longer needs it. Failing to do so will cause very odd behavior or even crashes. libcurl will need it until you call curl_easy_cleanup(3) or you set the same option again to use a different pointer.
Use CURLOPT_VERBOSE and CURLOPT_DEBUGFUNCTION to better debug/trace why errors happen.
If the library does not return an error, the buffer may not have been touched. Do not rely on the contents in those cases.
- CURLOPT_STDERR
- Pass a FILE * as parameter. Tell libcurl to use this stream instead of stderr when showing the progress meter and displaying CURLOPT_VERBOSE data.
- CURLOPT_FAILONERROR
- A parameter set to 1 tells the library to fail silently if the HTTP code returned is equal to or larger than 400. The default action would be to return the page normally, ignoring that code.
This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407).
You might get some amounts of headers transferred before this situation is detected, like when a "100-continue" is received as a response to a POST/PUT and a 401 or 407 is received immediately afterwards.
NETWORK OPTIONS
- CURLOPT_URL
- Pass in a pointer to the actual URL to deal with. The parameter should be a char * to a zero terminated string which must be URL-encoded in the following format:
For a greater explanation of the format please see RFC3986.
If the given URL lacks the scheme, or protocol, part ("http://" or "ftp://" etc), libcurl will attempt to resolve which protocol to use based on the given host mame. If the protocol is not supported, libcurl will return (CURLE_UNSUPPORTED_PROTOCOL) when you call curl_easy_perform(3) or curl_multi_perform(3). Use curl_version_info(3) for detailed information on which protocols are supported.
The host part of the URL contains the address of the server that you want to connect to. This can be the fully qualified domain name of the server, the local network name of the machine on your network or the IP address of the server or machine represented by either an IPv4 or IPv6 address. For example:
http://[2001:1890:1112:1::20]/
It is also possible to specify the user name and password as part of the host, for some protocols, when connecting to servers that require authentication.
For example the following types of authentication support this:
http://user:password@www.example.com
ftp://user:password@ftp.example.com
pop3://user:password [at] mail.example.com
The port is optional and when not specified libcurl will use the default port based on the determined or specified protocol: 80 for HTTP, 21 for FTP and 25 for SMTP, etc. The following examples show how to specify the port:
http://www.example.com:8080/ – This will connect to a web server using port 8080 rather than 80.
smtp://mail.example.com:587/ – This will connect to a SMTP server on the alternative mail port.
The path part of the URL is protocol specific and whilst some examples are given below this list is not conclusive:
HTTPThe path part of a HTTP request specifies the file to retrieve and from what directory. If the directory is not specified then the web server’s root directory is used. If the file is omitted then the default document will be retrieved for either the directory specified or the root directory. The exact resource returned for each URL is entirely dependent on the server’s configuration.
http://www.example.com – This gets the main page from the web server.
http://www.example.com/index.html – This returns the main page by explicitly requesting it.
http://www.example.com/contactus/ – This returns the default document from the contactus directory.
FTPThe path part of an FTP request specifies the file to retrieve and from what directory. If the file part is omitted then libcurl downloads the directory listing for the directory specified. If the directory is omitted then the directory listing for the root / home directory will be returned.
ftp://ftp.example.com – This retrieves the directory listing for the root directory.
ftp://ftp.example.com/readme.txt – This downloads the file readme.txt from the root directory.
ftp://ftp.example.com/libcurl/readme.txt – This downloads readme.txt from the libcurl directory.
ftp://user:password@ftp.example.com/readme.txt – This retrieves the readme.txt file from the user’s home directory. When a username and password is specified, everything that is specified in the path part is relative to the user’s home directory. To retrieve files from the root directory or a directory underneath the root directory then the absolute path must be specified by prepending an additional forward slash to the beginning of the path.
ftp://user:password@ftp.example.com//readme.txt – This retrieves the readme.txt from the root directory when logging in as a specified user.
SMTPThe path part of a SMTP request specifies the host name to present during communication with the mail server. If the path is omitted then libcurl will attempt to resolve the local computer’s host name. However, this may not return the fully qualified domain name that is required by some mail servers and specifying this path allows you to set an alternative name, such as your machine’s fully qualified domain name, which you might have obtained from an external function such as gethostname or getaddrinfo.
smtp://mail.example.com – This connects to the mail server at example.com and sends your local computer’s host name in the HELO / EHLO command.
smtp://mail.example.com/client.example.com – This will send client.example.com in the HELO / EHLO command to the mail server at example.com.
POP3The path part of a POP3 request specifies the mailbox (message) to retrieve. If the mailbox is not specified then a list of waiting messages is returned instead.
pop3://user:password [at] mail.example.com – This lists the available messages pop3://user:password [at] mail.example.com/1 – This retrieves the first message
SCPThe path part of a SCP request specifies the file to retrieve and from what directory. The file part may not be omitted. The file is taken as an absolute path from the root directory on the server. To specify a path relative to the user’s home directory on the server, prepend ~/ to the path portion. If the user name is not embedded in the URL, it can be set with the CURLOPT_USERPWD or
CURLOPT_USERNAMEoption.scp://user@example.com/etc/issue – This specifies the file /etc/issue
scp://example.com/~/my-file – This specifies the file my-file in the user’s home directory on the server
SFTPThe path part of a SFTP request specifies the file to retrieve and from what directory. If the file part is omitted then libcurl downloads the directory listing for the directory specified. If the path ends in a / then a directory listing is returned instead of a file. If the path is omitted entirely then the directory listing for the root / home directory will be returned. If the user name is not embedded in the URL, it can be set with the CURLOPT_USERPWD or
CURLOPT_USERNAMEoption.sftp://user:password@example.com/etc/issue – This specifies the file /etc/issue
sftp://user@example.com/~/my-file – This specifies the file my-file in the user’s home directory
sftp://ssh.example.com/~/Documents/ – This requests a directory listing of the Documents directory under the user’s home directory
LDAPThe path part of a LDAP request can be used to specify the: Distinguished Name, Attributes, Scope, Filter and Extension for a LDAP search. Each field is separated by a question mark and when that field is not required an empty string with the question mark separator should be included.
ldap://ldap.example.com/o=My%20Organisation – This will perform a LDAP search with the DN as My Organisation.
ldap://ldap.example.com/o=My%20Organisation?postalAddress – This will perform the same search but will only return postalAddress attributes.
ldap://ldap.example.com/?rootDomainNamingContext – This specifies an empty DN and requests information about the rootDomainNamingContext attribute for an Active Directory server.
For more information about the individual components of a LDAP URL please see RFC4516.
NOTESStarting with version 7.20.0, the fragment part of the URI will not be sent as part of the path, which was previously the case.
CURLOPT_URL is the only option that
mustbe set before curl_easy_perform(3) is called.CURLOPT_PROTOCOLS can be used to limit what protocols libcurl will use for this transfer, independent of what libcurl has been compiled to support. That may be useful if you accept the URL from an external source and want to limit the accessibility.
- CURLOPT_PROTOCOLS
- Pass a long that holds a bitmask of CURLPROTO_* defines. If used, this bitmask limits what protocols libcurl may use in the transfer. This allows you to have a libcurl built to support a wide range of protocols but still limit specific transfers to only be allowed to use a subset of them. By default libcurl will accept all protocols it supports. See also CURLOPT_REDIR_PROTOCOLS. (Added in 7.19.4)
- CURLOPT_REDIR_PROTOCOLS
- Pass a long that holds a bitmask of CURLPROTO_* defines. If used, this bitmask limits what protocols libcurl may use in a transfer that it follows to in a redirect when CURLOPT_FOLLOWLOCATION is enabled. This allows you to limit specific transfers to only be allowed to use a subset of protocols in redirections. By default libcurl will allow all protocols except for FILE and SCP. This is a difference compared to pre-7.19.4 versions which unconditionally would follow to all protocols supported. (Added in 7.19.4)
- CURLOPT_PROXY
- Set HTTP proxy to use. The parameter should be a char * to a zero terminated string holding the host name or dotted IP address. To specify port number in this string, append :[port] to the end of the host name. The proxy string may be prefixed with [protocol]:// since any such prefix will be ignored. The proxy’s port number may optionally be specified with the separate option. If not specified, libcurl will default to using port 1080 for proxies. CURLOPT_PROXYPORT.
When you tell the library to use a HTTP proxy, libcurl will transparently convert operations to HTTP even if you specify an FTP URL etc. This may have an impact on what other features of the library you can use, such as CURLOPT_QUOTE and similar FTP specifics that don’t work unless you tunnel through the HTTP proxy. Such tunneling is activated with CURLOPT_HTTPPROXYTUNNEL.
libcurl respects the environment variables
http_proxy,ftp_proxy,all_proxyetc, if any of those are set. The CURLOPT_PROXY option does however override any possibly set environment variables.Setting the proxy string to "" (an empty string) will explicitly disable the use of a proxy, even if there is an environment variable set for it.
Since 7.14.1, the proxy host string given in environment variables can be specified the exact same way as the proxy can be set with CURLOPT_PROXY, include protocol prefix (http://) and embedded user + password.
Since 7.21.7, the proxy string may be specified with a protocol:// prefix to specify alternative proxy protocols. Use socks4://, socks4a://, socks5:// or socks5h:// (the last one to enable socks5 and asking the proxy to do the resolving, also known as CURLPROXY_SOCKS5_HOSTNAME type) to request the specific SOCKS version to be used. No protocol specified, http:// and all others will be treated as HTTP proxies.
- CURLOPT_PROXYPORT
- Pass a long with this option to set the proxy port to connect to unless it is specified in the proxy string CURLOPT_PROXY.
- CURLOPT_PROXYTYPE
- Pass a long with this option to set type of the proxy. Available options for this are CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 (added in 7.19.4), CURLPROXY_SOCKS4 (added in 7.10), CURLPROXY_SOCKS5, CURLPROXY_SOCKS4A (added in 7.18.0) and CURLPROXY_SOCKS5_HOSTNAME (added in 7.18.0). The HTTP type is default. (Added in 7.10)
If you set
CURLOPT_PROXYTYPEto CURLPROXY_HTTP_1_0, it will only affect how libcurl speaks to a proxy when CONNECT is used. The HTTP version used for "regular" HTTP requests is instead controlled with CURLOPT_HTTP_VERSION. - CURLOPT_NOPROXY
- Pass a pointer to a zero terminated string. The string consists of a comma separated list of host names that do not require a proxy to get reached, even if one is specified. The only wildcard available is a single * character, which matches all hosts, and effectively disables the proxy. Each name in this list is matched as either a domain which contains the hostname, or the hostname itself. For example, example.com would match example.com, example.com:80, and www.example.com, but not www.notanexample.com. (Added in 7.19.4)
- CURLOPT_HTTPPROXYTUNNEL
- Set the parameter to 1 to make the library tunnel all operations through a given HTTP proxy. There is a big difference between using a proxy and to tunnel through it. If you don’t know what this means, you probably don’t want this tunneling option.
- CURLOPT_SOCKS5_GSSAPI_SERVICE
- Pass a char * as parameter to a string holding the name of the service. The default service name for a SOCKS5 server is rcmd/server-fqdn. This option allows you to change it. (Added in 7.19.4)
- CURLOPT_SOCKS5_GSSAPI_NEC
- Pass a long set to 1 to enable or 0 to disable. As part of the gssapi negotiation a protection mode is negotiated. The RFC1961 says in section 4.3/4.4 it should be protected, but the NEC reference implementation does not. If enabled, this option allows the unprotected exchange of the protection mode negotiation. (Added in 7.19.4).
- CURLOPT_INTERFACE
- Pass a char * as parameter. This sets the interface name to use as outgoing network interface. The name can be an interface name, an IP address, or a host name.
Starting with 7.24.0: If the parameter starts with "if!" then it is treated as only as interface name and no attempt will ever be named to do treat it as an IP address or to do name resolution on it. If the parameter starts with "host!" it is treated as either an IP address or a hostname. Hostnames are resolved synchronously. Using the if! format is highly recommended when using the multi interfaces to avoid allowing the code to block. If "if!" is specified but the parameter does not match an existing interface, CURLE_INTERFACE_FAILED is returned.
- CURLOPT_LOCALPORT
- Pass a long. This sets the local port number of the socket used for connection. This can be used in combination with CURLOPT_INTERFACE and you are recommended to use CURLOPT_LOCALPORTRANGE as well when this is set. Valid port numbers are 1 – 65535. (Added in 7.15.2)
- CURLOPT_LOCALPORTRANGE
- Pass a long. This is the number of attempts libcurl will make to find a working local port number. It starts with the given CURLOPT_LOCALPORT and adds one to the number for each retry. Setting this to 1 or below will make libcurl do only one try for the exact port number. Port numbers by nature are scarce resources that will be busy at times so setting this value to something too low might cause unnecessary connection setup failures. (Added in 7.15.2)
- CURLOPT_DNS_CACHE_TIMEOUT
- Pass a long, this sets the timeout in seconds. Name resolves will be kept in memory for this number of seconds. Set to zero to completely disable caching, or set to -1 to make the cached entries remain forever. By default, libcurl caches this info for 60 seconds.
The name resolve functions of various libc implementations don’t re-read name server information unless explicitly told so (for example, by calling res_init(3)). This may cause libcurl to keep using the older server even if DHCP has updated the server info, and this may look like a DNS cache issue to the casual libcurl-app user.
- CURLOPT_DNS_USE_GLOBAL_CACHE
- Pass a long. If the value is 1, it tells curl to use a global DNS cache that will survive between easy handle creations and deletions. This is not thread-safe and this will use a global variable.
WARNING:this option is considered obsolete. Stop using it. Switch over to using the share interface instead! See CURLOPT_SHARE and curl_share_init(3). - CURLOPT_BUFFERSIZE
- Pass a long specifying your preferred size (in bytes) for the receive buffer in libcurl. The main point of this would be that the write callback gets called more often and with smaller chunks. This is just treated as a request, not an order. You cannot be guaranteed to actually get the given size. (Added in 7.10)
This size is by default set as big as possible (CURL_MAX_WRITE_SIZE), so it only makes sense to use this option if you want it smaller.
- CURLOPT_PORT
- Pass a long specifying what remote port number to connect to, instead of the one specified in the URL or the default port for the used protocol.
- CURLOPT_TCP_NODELAY
- Pass a long specifying whether the TCP_NODELAY option is to be set or cleared (1 = set, 0 = clear). The option is cleared by default. This will have no effect after the connection has been established.
Setting this option will disable TCP’s Nagle algorithm. The purpose of this algorithm is to try to minimize the number of small packets on the network (where "small packets" means TCP segments less than the Maximum Segment Size (MSS) for the network).
Maximizing the amount of data sent per TCP segment is good because it amortizes the overhead of the send. However, in some cases (most notably telnet or rlogin) small segments may need to be sent without delay. This is less efficient than sending larger amounts of data at a time, and can contribute to congestion on the network if overdone.
- CURLOPT_ADDRESS_SCOPE
- Pass a long specifying the scope_id value to use when connecting to IPv6 link-local or site-local addresses. (Added in 7.19.0)
- CURLOPT_TCP_KEEPALIVE
- Pass a long. If set to 1, TCP keepalive probes will be sent. The delay and frequency of these probes can be controlled by the CURLOPT_TCP_KEEPIDLE and CURLOPT_TCP_KEEPINTVL options, provided the operating system supports them. Set to 0 (default behavior) to disable keepalive probes (Added in 7.25.0).
- CURLOPT_TCP_KEEPIDLE
- Pass a long. Sets the delay, in seconds, that the operating system will wait while the connection is idle before sending keepalive probes. Not all operating systems support this option. (Added in 7.25.0)
- CURLOPT_TCP_KEEPINTVL
- Pass a long. Sets the interval, in seconds, that the operating system will wait between sending keepalive probes. Not all operating systems support this option. (Added in 7.25.0)
- CURLOPT_UNIX_SOCKET_PATH
- Pass a path to a UNIX domain socket. This enables the use of UNIX domain sockets as connection end point and sets the path to path. If path is NULL, then UNIX domain sockets are disabled. An empty string will result in an error at some point.
When enabled, cURL will connect to the UNIX domain socket instead of establishing a TCP connection to a host. Since no TCP connection is established, cURL does not need to resolve the DNS hostname in the URL.
The maximum path length on Cygwin, Linux and Solaris is 107. On other platforms might be even less.
NAMES and PASSWORDS OPTIONS (Authentication)
- CURLOPT_NETRC
- This parameter controls the preference of libcurl between using user names and passwords from your ~/.netrc file, relative to user names and passwords in the URL supplied with CURLOPT_URL.
libcurl uses a user name (and supplied or prompted password) supplied with CURLOPT_USERPWD in preference to any of the options controlled by this parameter.
Pass a long, set to one of the values described below.
-
- CURL_NETRC_OPTIONAL
- The use of your ~/.netrc file is optional, and information in the URL is to be preferred. The file will be scanned for the host and user name (to find the password only) or for the host only, to find the first user name and password after that machine, which ever information is not specified in the URL.
Undefined values of the option will have this effect.
- CURL_NETRC_IGNORED
- The library will ignore the file and use only the information in the URL.
This is the default.
- CURL_NETRC_REQUIRED
- This value tells the library that use of the file is required, to ignore the information in the URL, and to search the file for the host only.
Only machine name, user name and password are taken into account (init macros and similar things aren’t supported).
libcurl does not verify that the file has the correct properties set (as the standard Unix ftp client does). It should only be readable by user.
- CURLOPT_NETRC_FILE
- Pass a char * as parameter, pointing to a zero terminated string containing the full path name to the file you want libcurl to use as .netrc file. If this option is omitted, and CURLOPT_NETRC is set, libcurl will attempt to find a .netrc file in the current user’s home directory. (Added in 7.10.9)
- CURLOPT_USERPWD
- Pass a char * as parameter, which should be [user name]:[password] to use for the connection. Use CURLOPT_HTTPAUTH to decide the authentication method.
When using NTLM, you can set the domain by prepending it to the user name and separating the domain and name with a forward (/) or backward slash (\). Like this: "domain/user:password" or "domain
