{"record":{"id":"99ed212b4f31ecda","repo":"restsharp/RestSharp","slug":"invalid-content-type-string","errorCode":null,"errorMessage":"Invalid content type string","messagePattern":"Invalid content type string","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/ContentType.cs","lineNumber":24,"sourceCode":"//\n//     http://www.apache.org/licenses/LICENSE-2.0\n//\n//   Unless required by applicable law or agreed to in writing, software\n//   distributed under the License is distributed on an \"AS IS\" BASIS,\n//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n//   See the License for the specific language governing permissions and\n//   limitations under the License. \n\nusing System.Net.Http.Headers;\n\nnamespace RestSharp;\n\npublic delegate bool SupportsContentType(ContentType contentType);\n\npublic class ContentType : IEquatable<ContentType> {\n    ContentType(string contentType) {\n        var ct = Ensure.NotEmptyString(contentType, nameof(contentType));\n        if (!ct.Contains('/')) throw new ArgumentException(\"Invalid content type string\", nameof(contentType));\n\n        _value = ct;\n    }\n\n    public static readonly ContentType Json           = \"application/json\";\n    public static readonly ContentType Xml            = \"application/xml\";\n    public static readonly ContentType Plain          = \"text/plain\";\n    public static readonly ContentType Csv            = \"text/csv\";\n    public static readonly ContentType Binary         = \"application/octet-stream\";\n    public static readonly ContentType GZip           = \"application/x-gzip\";\n    public static readonly ContentType FormUrlEncoded = \"application/x-www-form-urlencoded\";\n    public static readonly ContentType Undefined      = \"undefined/undefined\";\n\n    public string Value => _value == Undefined._value ? Plain._value : _value;\n\n    public static ContentType FromDataFormat(DataFormat dataFormat) => DataFormatMap[dataFormat];\n\n    public override string ToString() => Value;","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/ContentType.cs#L6-L42","documentation":"Thrown by the ContentType private constructor when the supplied content-type string does not contain a '/' character. Every valid MIME type has the form type/subtype, so the absence of a slash indicates an malformed value.","triggerScenarios":"Implicit or explicit conversion from a string to ContentType where the string lacks a slash, e.g. ContentType contentType = \"json\" or \"application\" or a bare token. The implicit string operator invokes the constructor which validates.","commonSituations":"Passing a bare format name ('json', 'xml') instead of a full MIME type; reading content type from a header that was truncated; user input mapped directly to ContentType without normalization.","solutions":["Provide a full MIME type string such as 'application/json' or 'text/xml'.","Use the predefined ContentType constants (ContentType.Json, ContentType.Xml, etc.) instead of raw strings.","Validate input strings contain a '/' before assigning to a ContentType field."],"exampleFix":"// before\nContentType ct = \"json\";\n\n// after\nContentType ct = ContentType.Json;\n// or\nContentType ct = \"application/json\";","handlingStrategy":"validation","validationCode":"if (!value.Contains('/')) throw new ArgumentException(\"Content type must be in type/subtype form\", nameof(value));","typeGuard":"static bool IsValidContentTypeString(string s) => !string.IsNullOrWhiteSpace(s) && s.Contains('/');","tryCatchPattern":"try { ContentType ct = value; } catch (ArgumentException ex) when (ex.Message.Contains(\"Invalid content type string\")) { /* default to a known ContentType constant */ }","preventionTips":["Use the predefined ContentType constants (Json, Xml, etc.) instead of raw strings.","Validate external strings contain '/' before assigning to ContentType."],"tags":["content-type","validation","mime"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}