curl / Mailing Lists / curl-users / Single Mail
Buy commercial curl support. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder Daniel himself.

CURLOPT_HTTPHEADER appends headers but does not delete old ones

From: Mac-Fly via curl-users <curl-users_at_lists.haxx.se>
Date: Fri, 15 May 2026 17:48:17 +0200

Dear all,

I have a strange effect that I see in one of my projects where I am setting up header entries provided as a list derived from user settings.
I am using the code below that is supposed to delete old header entries, then collect the ones to be applied and finally applies the entries.
To my understanding, this should only include the headers that I add in this respective call.
Instead, what happens is that the previous ones remain active and the ones I append in further calls to that function are being added additionally.
I was following the example in: https://curl.se/libcurl/c/CURLOPT_HTTPHEADER.html

Am I missing something? Any help is greatly appreciated, maybe you spot something?

Here is the code in question.
Remark: "curl_hdr_data" is a copy kept from a previous call, "headers" is a vector of strings to add to the header (line-by-line basically).

  // Delete previously applied headers
  struct curl_slist* curl_hdr_list = static_cast<struct curl_slist*>(curl_hdr_data);
  if (nullptr!=curl_hdr_list)
  {
    curl_slist_free_all(curl_hdr_list); // Free any previous data from the list
    curl_hdr_list = nullptr;
  }

  // Assemble all headers provided
  for (size_t i=0; i<headers.GetCount(); i++)
  {
    std::string stl_header = headers.Item(i).ToStdString();
    curl_hdr_list = curl_slist_append( curl_hdr_list, stl_header.c_str() );
  }

  // Apply all headers provided
  CURLcode cc = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_hdr_list);
  if (CURLE_OK!=cc)
  {
    return false;
  }

Best regards,
Morten.
-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-users
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2026-05-15