{"record":{"id":"d29d9be5fc5dbb0c","repo":"reactiveui/refit","slug":"the-character-set-provided-in-contenttype-is-inval","errorCode":null,"errorMessage":"The character set provided in ContentType is invalid.","messagePattern":"The character set provided in ContentType is invalid\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Refit.Newtonsoft.Json/NewtonsoftJsonContentSerializer.cs","lineNumber":172,"sourceCode":"    {\n        var charset = content.Headers.ContentType?.CharSet;\n        if (charset is null)\n        {\n            return null;\n        }\n\n        try\n        {\n            if (charset.Length > QuotePairLength && charset[0] == '\"' && charset[^1] == '\"')\n            {\n                charset = charset.Substring(1, charset.Length - QuotePairLength);\n            }\n\n            return Encoding.GetEncoding(charset);\n        }\n        catch (ArgumentException e)\n        {\n            throw new InvalidOperationException(\"The character set provided in ContentType is invalid.\", e);\n        }\n    }\n}\n","sourceCodeStart":154,"sourceCodeEnd":176,"githubUrl":"https://github.com/reactiveui/refit/blob/b455f65ecc4c97d092317e349cb775f9cfc6bcdf/src/Refit.Newtonsoft.Json/NewtonsoftJsonContentSerializer.cs#L154-L176","documentation":"The Newtonsoft.Json content serializer reads the charset from a response's Content-Type header and tries Encoding.GetEncoding(charset) after stripping one pair of surrounding quotes. When the platform cannot resolve that charset, the underlying ArgumentException is caught and rethrown as InvalidOperationException. It is a runtime, per-response error driven entirely by what the server sent.","triggerScenarios":"A response arrives with a Content-Type charset that is malformed or unregistered, e.g. 'charset=utf-8-sig', 'charset=\"utf-8' (unbalanced quote), a vendor label, or a charset absent from the running .NET runtime's registered encodings.","commonSituations":"A third-party/legacy server emits a non-standard charset; a CDN rewrites headers; a misconfigured reverse proxy injects charset; running on a trimmed AOT build where the encoding provider for the charset was not registered.","solutions":["Wrap response calls in try/catch and, on this exception, re-read the content as the bytes/string and decode with a known Encoding (e.g. Encoding.UTF8) ignoring the header.","Intercept the response in a DelegatingHandler and rewrite/normalize Content-Type charset before deserialization.","If the charset is legitimate (e.g. a code-page) register the encoding provider at startup: Encoding.RegisterProvider(CodePagesEncodingProvider.Instance).","Switch to Refit.SystemTextJson if Newtonsoft charset handling is not required."],"exampleFix":"// before\nvar result = await api.GetUserAsync(id); // throws on bad charset\n\n// after\nUserDto result;\ntry {\n    result = await api.GetUserAsync(id);\n} catch (InvalidOperationException ex) when (ex.Message.Contains(\"character set\")) {\n    // re-fetch and decode ignoring the header charset\n    using var raw = await client.GetAsync($\"/users/{id}\");\n    var body = await raw.Content.ReadAsByteArrayAsync();\n    result = JsonConvert.DeserializeObject<UserDto>(Encoding.UTF8.GetString(body));\n}","handlingStrategy":"try-catch","validationCode":"// Pre-validate a charset string before relying on it (if you read it yourself).\nstatic bool IsKnownCharset(string? charset) {\n    if (string.IsNullOrWhiteSpace(charset)) return true;\n    try { Encoding.GetEncoding(charset.Trim('\"')); return true; }\n    catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return await api.GetThingAsync(id);\n}\ncatch (InvalidOperationException ex) when (ex.InnerException is ArgumentException && ex.Message.Contains(\"character set\")) {\n    // Re-read bytes and decode with a known encoding, ignoring the bad header.\n}","preventionTips":["Register encoding providers at startup: Encoding.RegisterProvider(CodePagesEncodingProvider.Instance).","Normalize Content-Type charset in a DelegatingHandler before deserialization.","Consider Refit.SystemTextJson if you do not need Newtonsoft charset parsing."],"tags":["refit","newtonsoft-json","encoding","charset","http-response","runtime"],"backgroundTag":null,"analyzedSha":"b455f65ecc4c97d092317e349cb775f9cfc6bcdf","analyzedAt":"2026-08-13T21:20:57.878Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}