restsharp/RestSharp · error · ArgumentException

Duplicate header names exist: {duplicateKeys}

Error message

Duplicate header names exist: {duplicateKeys}

What it means

CheckAndThrowsDuplicateKeys throws ArgumentException when the collection passed to AddHeaders or AddOrUpdateHeaders contains two or more entries with the same header name (compared case-insensitively via ToUpperInvariant). Duplicate names within a single AddHeaders call are ambiguous, so RestSharp rejects the whole batch rather than silently overwriting.

Source

Thrown at src/RestSharp/Request/RestRequestExtensions.Headers.cs:118

            CheckAndThrowsDuplicateKeys(headers);

            foreach (var pair in headers) {
                request.AddOrUpdateHeader(pair.Key, pair.Value);
            }

            return request;
        }
    }

    static void CheckAndThrowsDuplicateKeys(ICollection<KeyValuePair<string, string>> headers) {
        var duplicateKeys = headers
            .GroupBy(pair => pair.Key.ToUpperInvariant())
            .Where(group => group.Count() > 1)
            .Select(group => group.Key)
            .ToList();

        if (duplicateKeys.Count > 0) {
            throw new ArgumentException($"Duplicate header names exist: {string.Join(", ", duplicateKeys)}");
        }
    }
}

View on GitHub (pinned to 6a50821692)

Solutions

  1. Dedupe the header collection by name (case-insensitive) before calling AddHeaders.
  2. Use AddOrUpdateHeader individually if you intend last-wins semantics.
  3. Source headers from a case-insensitive dictionary (StringComparer.OrdinalIgnoreCase) so duplicates cannot accumulate.

Example fix

// before
var headers = new List<KeyValuePair<string,string>> {
    new("Accept", "application/json"),
    new("accept", "text/plain"), // duplicate, throws
};
request.AddHeaders(headers);

// after
var headers = headers
    .GroupBy(h => h.Key, StringComparer.OrdinalIgnoreCase)
    .Select(g => g.Last())
    .ToList();
request.AddHeaders(headers);
Defensive patterns

Strategy: validation

Validate before calling

var deduped = headers
    .GroupBy(h => h.Key, StringComparer.OrdinalIgnoreCase)
    .Select(g => g.Last())
    .ToList();
var dupes = deduped.GroupBy(h => h.Key.ToUpperInvariant()).Where(g => g.Count() > 1).ToList();
if (dupes.Count > 0) throw new ArgumentException("Duplicate headers: " + string.Join(", ", dupes.Select(g => g.Key)));
request.AddHeaders(deduped);

Try / catch

try {
    request.AddHeaders(headers);
}
catch (ArgumentException ex) when (ex.Message.Contains("Duplicate header names")) {
    // dedupe and retry, or surface a config error
}

Prevention

When it happens

Trigger: Calling request.AddHeaders(pairs) or request.AddOrUpdateHeaders(pairs) where the ICollection<KeyValuePair<string,string>> has repeated keys, including the same name in different cases (e.g. "Content-Type" and "content-type").

Common situations: Building headers from a Dictionary that was merged from multiple sources; deserializing headers from config/JSON where duplicates sneak in; mixing casing conventions ("Accept" vs "ACCEPT").

Related errors


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/462d146a49f43447. Report an issue: GitHub.