CURLOPT_PREREQFUNCTION(3)
okay bestie, welcome to the glow-up of CURLOPT_PREREQFUNCTION(3) from the Library Functions Manual β this is the libcurl callback that fires when a connection is up but before the actual request goes out. π«Ά main character callback energy, fr fr.
πΈ NAME πΈ
CURLOPT_PREREQFUNCTION β the user callback that gets called when a connection has been established, but before a request has been made. it's giving βwe're connected, but hold the requestβ energy β¨
β¨ SYNOPSIS β¨
no cap, here's the prototype and the return codes:
#include <curl/curl.h>
/* These are the return codes for the pre-request callback. */
#define CURL_PREREQFUNC_OK 0
#define CURL_PREREQFUNC_ABORT 1 /* fail the entire transfer */
int prereq_callback(void *clientp,
char *conn_primary_ip,
char *conn_local_ip,
int conn_primary_port,
int conn_local_port);
CURLcode curl_easy_setopt(CURL *handle, CURLOPT_PREREQFUNCTION, prereq_callback);
π¦ DESCRIPTION π¦
Pass a pointer to your callback function, and make sure it matches the prototype shown above. that's the vibe check. π
This function gets called by libcurl after a connection has been established or a connection has been reused (including any SSL handshaking), but before any request is actually made on the connection. So for HTTP, this callback is called once a connection has been established to the server, but before a GET/HEAD/POST/etc request has been sent. literally βconnection secured, request pendingβ π
This function may be called multiple times if redirections are enabled and are being followed (see CURLOPT_FOLLOWLOCATION(3)). yes bestie, she can show up more than once π
The callback function must return CURL_PREREQFUNC_OK on success, or CURL_PREREQFUNC_ABORT to cause the transfer to fail with result CURLE_ABORTED_BY_CALLBACK.
returningCURL_PREREQFUNC_ABORTis not a subtle βnoβ; it causes the transfer to fail with resultCURLE_ABORTED_BY_CALLBACK. dramatic? yes. accurate? also yes. π
This function is passed the following arguments:
conn_primary_ipβ a pointer to a null-terminated C string containing the primary IP of the remote server established with this connection. For FTP, this is the IP for the control connection. IPv6 addresses are represented without surrounding brackets. no brackets, periodt πconn_local_ipβ a pointer to a null-terminated C string containing the originating IP for this connection. IPv6 addresses are represented without surrounding brackets.conn_primary_portβ the primary port number on the remote server established with this connection. For FTP, this is the port for the control connection. This can be a TCP or a UDP port number depending on the protocol.conn_local_portβ the originating port number for this connection. This can be a TCP or a UDP port number depending on the protocol.clientpβ the pointer you set withCURLOPT_PREREQDATA(3).
π DEFAULT π
NULL β the default is literally NULL. no callback set, no prereq check-in. π
π PROTOCOLS π
This functionality affects all supported protocols β no gatekeeping, all of them. β¨
π EXAMPLE π
here's a lil example that prints where the connection landed:
struct priv {
void *custom;
};
static int prereq_callback(void *clientp,
char *conn_primary_ip,
char *conn_local_ip,
int conn_primary_port,
int conn_local_port)
{
printf("Connection made to %s:%d\n", conn_primary_ip, conn_primary_port);
return CURL_PREREQFUNC_OK;
}
int main(void)
{
struct priv prereq_data;
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION, prereq_callback);
curl_easy_setopt(curl, CURLOPT_PREREQDATA, &prereq_data);
curl_easy_perform(curl);
}
}
π AVAILABILITY π
Added in curl 7.80.0 β so she's been around, but she still ate. π
β RETURN VALUE β
curl_easy_setopt(3) returns a CURLcode indicating success or error.
CURLE_OK (0) means everything was OK, non-zero means an error occurred, see libcurl-errors(3). that's the whole return-value tea. π«
π§ SEE ALSO π§
xoxo, from your girl libcurl, dated 2026-10-12 β catch you in the next manpage π
β¨
π¦ where this page comes from
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- fedora 44: libcurl-devel 8.18.0-14.fc44 Β· curl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms