restsharp/RestSharp · error · ArgumentException

Invalid content type string

Error message

Invalid content type string

What it means

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.

Source

Thrown at src/RestSharp/ContentType.cs:24

//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License. 

using System.Net.Http.Headers;

namespace RestSharp;

public delegate bool SupportsContentType(ContentType contentType);

public class ContentType : IEquatable<ContentType> {
    ContentType(string contentType) {
        var ct = Ensure.NotEmptyString(contentType, nameof(contentType));
        if (!ct.Contains('/')) throw new ArgumentException("Invalid content type string", nameof(contentType));

        _value = ct;
    }

    public static readonly ContentType Json           = "application/json";
    public static readonly ContentType Xml            = "application/xml";
    public static readonly ContentType Plain          = "text/plain";
    public static readonly ContentType Csv            = "text/csv";
    public static readonly ContentType Binary         = "application/octet-stream";
    public static readonly ContentType GZip           = "application/x-gzip";
    public static readonly ContentType FormUrlEncoded = "application/x-www-form-urlencoded";
    public static readonly ContentType Undefined      = "undefined/undefined";

    public string Value => _value == Undefined._value ? Plain._value : _value;

    public static ContentType FromDataFormat(DataFormat dataFormat) => DataFormatMap[dataFormat];

    public override string ToString() => Value;

View on GitHub (pinned to 6a50821692)

Solutions

  1. Provide a full MIME type string such as 'application/json' or 'text/xml'.
  2. Use the predefined ContentType constants (ContentType.Json, ContentType.Xml, etc.) instead of raw strings.
  3. Validate input strings contain a '/' before assigning to a ContentType field.

Example fix

// before
ContentType ct = "json";

// after
ContentType ct = ContentType.Json;
// or
ContentType ct = "application/json";
Defensive patterns

Strategy: validation

Validate before calling

if (!value.Contains('/')) throw new ArgumentException("Content type must be in type/subtype form", nameof(value));

Type guard

static bool IsValidContentTypeString(string s) => !string.IsNullOrWhiteSpace(s) && s.Contains('/');

Try / catch

try { ContentType ct = value; } catch (ArgumentException ex) when (ex.Message.Contains("Invalid content type string")) { /* default to a known ContentType constant */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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