restsharp/RestSharp · error · ArgumentException

Non-string body found with unsupported content type

Error message

Non-string body found with unsupported content type

What it means

Thrown by RestRequestExtensions.AddBody(obj, contentType) when the object is neither a string nor a byte[] and the provided content type string contains neither 'json' nor 'xml'. AddBody uses content type sniffing to pick the right serializer; if it cannot classify the body, it refuses to guess.

Source

Thrown at src/RestSharp/Request/RestRequestExtensions.Body.cs:43

        /// <returns></returns>
        /// <exception cref="ArgumentException">Thrown if request body type cannot be resolved</exception>
        /// <remarks>This method will try to figure out the right content type based on the request data format and the provided content type</remarks>
        public RestRequest AddBody(object obj, ContentType? contentType = null) {
            if (contentType == null) {
                return request.RequestFormat switch {
                    DataFormat.Json   => request.AddJsonBody(obj, contentType),
                    DataFormat.Xml    => request.AddXmlBody(obj, contentType),
                    DataFormat.Binary => request.AddParameter(new BodyParameter("", obj, ContentType.Binary)),
                    _                 => request.AddParameter(new BodyParameter("", obj.ToString()!, ContentType.Plain))
                };
            }

            return
                obj is string str                  ? request.AddStringBody(str, contentType) :
                obj is byte[] bytes                ? request.AddParameter(new BodyParameter("", bytes, contentType, DataFormat.Binary)) :
                contentType.Value.Contains("xml")  ? request.AddXmlBody(obj, contentType) :
                contentType.Value.Contains("json") ? request.AddJsonBody(obj, contentType) :
                                                     throw new ArgumentException("Non-string body found with unsupported content type", nameof(obj));
        }

        /// <summary>
        /// Adds a string body and figures out the content type from the data format specified. You can, for example, add a JSON string
        /// using this method as request body, using DataFormat.Json/>
        /// </summary>
        /// <param name="body">String body</param>
        /// <param name="dataFormat"><see cref="DataFormat"/> for the content</param>
        /// <returns></returns>
        public RestRequest AddStringBody(string body, DataFormat dataFormat) {
            var contentType = ContentType.FromDataFormat(dataFormat);
            request.RequestFormat = dataFormat;
            return request.AddParameter(new BodyParameter("", body, contentType));
        }

        /// <summary>
        /// Adds a string body to the request using the specified content type.
        /// </summary>

View on GitHub (pinned to 6a50821692)

Solutions

  1. Use AddJsonBody or AddXmlBody directly instead of AddBody when you know the format.
  2. If the body is a string, call AddStringBody so it is treated as raw text.
  3. If the body is bytes, pass a byte[] so AddBody routes it as binary.
  4. Set request.RequestFormat first and call AddBody(obj) without a contentType so the DataFormat switch is used.

Example fix

// before
request.AddBody(myObject, ContentType.Plain); // throws

// after
request.AddJsonBody(myObject);
Defensive patterns

Strategy: type-guard

Validate before calling

switch (obj) {
    case string s:
        request.AddStringBody(s, contentType!);
        break;
    case byte[] b:
        request.AddParameter(new BodyParameter("", b, contentType!, DataFormat.Binary));
        break;
    default:
        if (contentType!.Value.Contains("json")) request.AddJsonBody(obj, contentType);
        else if (contentType.Value.Contains("xml")) request.AddXmlBody(obj, contentType);
        else throw new ArgumentException($"Unsupported content type {contentType} for {obj.GetType()}");
        break;
}

Type guard

static string ResolveBody(object obj, ContentType contentType) => obj switch {
    string s  => "string",
    byte[]    => "binary",
    _ when contentType.Value.Contains("json") => "json",
    _ when contentType.Value.Contains("xml")  => "xml",
    _ => throw new ArgumentException("Unsupported content type for non-string body")
};

Prevention

When it happens

Trigger: Calling request.AddBody(obj, contentType) where obj is a class/struct (not string, not byte[]) and contentType is something like "text/plain", "application/octet-stream", "application/x-www-form-urlencoded", or any value without 'json'/'xml' in it.

Common situations: Using a custom content type for a typed object; migrating from AddJsonBody to AddBody without specifying DataFormat; passing ContentType.Plain or ContentType.Binary with a non-string object.

Related errors


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