curl_slist_append(3)

πŸ’– curl_slist_append(3) β€” the libcurl glow-up for your string list πŸ’–

okay so bestie, we're in the libcurl manual, section 3 (that's the library functions girlies ✨), and today's main character is curl_slist_append. she's lowkey the icon who just adds a string to your slist. periodt.


🌸 NAME 🌸

curl_slist_append β€” add a string to an slist πŸ’…

πŸ¦‹ SYNOPSIS πŸ¦‹

#include <curl/curl.h>

struct curl_slist *curl_slist_append(struct curl_slist *list,
                                     const char *string);

✨ DESCRIPTION ✨

okay so curl_slist_append(3) is literally out here appending a string to a linked list of strings. that's the whole vibe. πŸŽ€

you pass the existing list in as the first argument, and this function hands you back the new list. wanna create a brand new list? just pass in NULL in the list argument, easy. by the time this function returns, your specified string has been appended β€” she ate and left no crumbs. πŸ”₯

also important: curl_slist_append(3) copies the string. so it's not out here borrowing your pointer, it makes its own copy, no cap. but the string argument must be a valid string pointer and absolutely cannot be NULL β€” that's a hard no, bestie. 🚫

once you're done using it, the list should be freed with curl_slist_free_all(3). and listen β€” its nodes and the content they point to may not be altered outside this function. hands off, she's not yours anymore. πŸ™ˆ

🌈 PROTOCOLS 🌈

this functionality affects all supported protocols. all of them. every single one. we love a queen with range. πŸ‘‘

πŸ“ EXAMPLE πŸ“

int main(void)
{
  CURL *handle = curl_easy_init();
  struct curl_slist *slist = NULL;
  struct curl_slist *temp = NULL;

  slist = curl_slist_append(slist, "pragma:");

  if(!slist)
    return -1;

  temp = curl_slist_append(slist, "Accept:");

  if(!temp) {
    curl_slist_free_all(slist);
    return -1;
  }

  slist = temp;

  curl_easy_setopt(handle, CURLOPT_HTTPHEADER, slist);

  curl_easy_perform(handle);

  curl_slist_free_all(slist); /* free the list again */
}

like look at her go β€” she's appending "pragma:", checking for NULL like a responsible icon, then grabbing "Accept:" through a temp variable (iykyk, more on that down in return value πŸ‘€), setting CURLOPT_HTTPHEADER, performing, and then freeing the list again at the end because she cleans up after herself. we stan. 🌟

🧚 AVAILABILITY 🧚

added in curl 7.1 β€” an og, fr fr. ⭐

πŸ’œ RETURN VALUE πŸ’œ

if anything went wrong, you get a null pointer back. otherwise? the new list pointer, slay. πŸ’…

⚠️ important bestie warning: to avoid overwriting an existing non-empty list on failure, the new list should be returned to a temporary variable which you can test for NULL before updating the original list pointer. that's literally what temp was doing in the example above β€” she's not just cute, she's protecting your data. 😭🫢

πŸ‘‘ SEE ALSO πŸ‘‘


πŸ’Œ and that's the tea! written for the libcurl girlies, dated 2026-10-12, version info courtesy of libcurl itself. go forth and append responsibly, mwah πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from