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.

returning CURL_PREREQFUNC_ABORT is not a subtle β€œno”; it causes the transfer to fail with result CURLE_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 with CURLOPT_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