CURLOPT_HEADERDATA(3)

πŸ’– NAME πŸ’–

okay so this is the CURLOPT_HEADERDATA(3) page from the Library Functions Manual, and it's literally about a pointer you pass to your header callback. No cap.

  • CURLOPT_HEADERDATA β€” pointer to pass to header callback

✨ SYNOPSIS ✨

#include <curl/curl.h>

CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERDATA, void *pointer);

πŸ¦‹ DESCRIPTION πŸ¦‹

Bestie, you pass a pointer in here, and that pointer is used to write the header part of the received data to. It's giving "custom data delivery service" ✨.

If CURLOPT_WRITEFUNCTION(3) or CURLOPT_HEADERFUNCTION(3) is used, pointer is passed in to the respective callback. So your callback can be like "oh hey, this is my stuff" πŸ’….

If neither of those options are set, pointer must be a valid FILE * and it is used by a plain fwrite() to write headers to. Literally must be valid β€” no playing around.

🚨 warning, bestie: if you are using libcurl as a Windows DLL, you must use CURLOPT_WRITEFUNCTION(3) or CURLOPT_HEADERFUNCTION(3) if you set this option or you might experience crashes. Not cute. Not slay. Use the callback.

πŸŽ€ DEFAULT πŸŽ€

NULL β€” iconic empty-handed start.


🌈 PROTOCOLS 🌈

This functionality affects all supported protocols. Every single one, periodt.


πŸ“ EXAMPLE πŸ“

Here's the code. Copy it exactly, don't freestyle the literals:

struct my_info {
  int shoesize;
  char *secret;
};

static size_t header_callback(char *buffer, size_t size,
                              size_t nitems, void *userdata)
{
  struct my_info *i = userdata;
  printf("shoe size: %d\n", i->shoesize);
  /* now this callback can access the my_info struct */

  return nitems * size;
}

int main(void)
{
  CURL *curl = curl_easy_init();
  if(curl) {
    struct my_info my = { 10, "the cookies are in the cupboard" };
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");

    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);

    /* pass in custom data to the callback */
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, &my);

    curl_easy_perform(curl);
  }
}

πŸ’œ AVAILABILITY πŸ’œ

Added in curl 7.10. Old-school but still iconic.


πŸ‘‘ 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). Fr fr.


πŸ”₯ SEE ALSO πŸ”₯


mwah πŸ’‹ this page's footer tea is: source/version libcurl, date 2026-10-12. Stay hydrated and validate your FILE *, bestie 🫢

πŸ“¦ where this page comes from