{"record":{"id":"462d146a49f43447","repo":"restsharp/RestSharp","slug":"duplicate-header-names-exist-duplicatekeys","errorCode":null,"errorMessage":"Duplicate header names exist: {duplicateKeys}","messagePattern":"Duplicate header names exist: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Request/RestRequestExtensions.Headers.cs","lineNumber":118,"sourceCode":"            CheckAndThrowsDuplicateKeys(headers);\n\n            foreach (var pair in headers) {\n                request.AddOrUpdateHeader(pair.Key, pair.Value);\n            }\n\n            return request;\n        }\n    }\n\n    static void CheckAndThrowsDuplicateKeys(ICollection<KeyValuePair<string, string>> headers) {\n        var duplicateKeys = headers\n            .GroupBy(pair => pair.Key.ToUpperInvariant())\n            .Where(group => group.Count() > 1)\n            .Select(group => group.Key)\n            .ToList();\n\n        if (duplicateKeys.Count > 0) {\n            throw new ArgumentException($\"Duplicate header names exist: {string.Join(\", \", duplicateKeys)}\");\n        }\n    }\n}","sourceCodeStart":100,"sourceCodeEnd":121,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Request/RestRequestExtensions.Headers.cs#L100-L121","documentation":"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.","triggerScenarios":"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\").","commonSituations":"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\").","solutions":["Dedupe the header collection by name (case-insensitive) before calling AddHeaders.","Use AddOrUpdateHeader individually if you intend last-wins semantics.","Source headers from a case-insensitive dictionary (StringComparer.OrdinalIgnoreCase) so duplicates cannot accumulate."],"exampleFix":"// before\nvar headers = new List<KeyValuePair<string,string>> {\n    new(\"Accept\", \"application/json\"),\n    new(\"accept\", \"text/plain\"), // duplicate, throws\n};\nrequest.AddHeaders(headers);\n\n// after\nvar headers = headers\n    .GroupBy(h => h.Key, StringComparer.OrdinalIgnoreCase)\n    .Select(g => g.Last())\n    .ToList();\nrequest.AddHeaders(headers);","handlingStrategy":"validation","validationCode":"var deduped = headers\n    .GroupBy(h => h.Key, StringComparer.OrdinalIgnoreCase)\n    .Select(g => g.Last())\n    .ToList();\nvar dupes = deduped.GroupBy(h => h.Key.ToUpperInvariant()).Where(g => g.Count() > 1).ToList();\nif (dupes.Count > 0) throw new ArgumentException(\"Duplicate headers: \" + string.Join(\", \", dupes.Select(g => g.Key)));\nrequest.AddHeaders(deduped);","typeGuard":null,"tryCatchPattern":"try {\n    request.AddHeaders(headers);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Duplicate header names\")) {\n    // dedupe and retry, or surface a config error\n}","preventionTips":["Source headers from a Dictionary<string,string> with StringComparer.OrdinalIgnoreCase so duplicates cannot exist.","Dedupe before calling AddHeaders/AddOrUpdateHeaders.","Use AddOrUpdateHeader individually for last-wins semantics."],"tags":["headers","request","validation","duplicate","argument"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}