restsharp/RestSharp · error · SerializationException

Request body serialized to null

Error message

Request body serialized to null

What it means

Thrown inside RequestContent.GetSerialized when the configured serializer for the body's DataFormat returns null from Serialize(body). A request body that serializes to null means there is no HTTP content to send, which RestSharp treats as a hard failure via SerializationException rather than sending an empty body.

Source

Thrown at src/RestSharp/Request/RequestContent.cs:107

        };

        HttpContent GetBinary() {
            var byteContent = new ByteArrayContent((body.Value as byte[])!);
            byteContent.Headers.ContentType = body.ContentType.AsMediaTypeHeaderValue;

            if (body.ContentEncoding != null) {
                byteContent.Headers.ContentEncoding.Clear();
                byteContent.Headers.ContentEncoding.Add(body.ContentEncoding);
            }

            return byteContent;
        }

        HttpContent GetSerialized() {
            var serializer = client.Serializers.GetSerializer(body.DataFormat);
            var content    = serializer.Serialize(body);

            if (content == null) throw new SerializationException("Request body serialized to null");

            var contentType = body.ContentType.Or(serializer.Serializer.ContentType);

            return new StringContent(content, client.Options.Encoding, contentType.Value);
        }
    }

    static bool BodyShouldBeMultipartForm(BodyParameter? bodyParameter) {
        if (bodyParameter == null) return false;

        var bodyContentType = bodyParameter.ContentType.OrValue(bodyParameter.Name);
        return bodyParameter.Name.IsNotEmpty() && bodyParameter.Name != bodyContentType;
    }

    string GetOrSetFormBoundary() => request.FormBoundary ?? (request.FormBoundary = Guid.NewGuid().ToString());

    MultipartFormDataContent CreateMultipartFormDataContent() {
        var boundary    = GetOrSetFormBoundary();

View on GitHub (pinned to 6a50821692)

Solutions

  1. Do not pass null as the body object; guard with a null check before AddJsonBody/AddXmlBody.
  2. If using a custom IRestSerializer, ensure Serialize never returns null (return an empty string or throw a meaningful exception instead).
  3. Verify the correct DataFormat/serializer is registered for the body type via ConfigureSerialization.
  4. For nullable bodies, branch: only add a body when the object is non-null.

Example fix

// before
request.AddJsonBody(maybeNullObject);

// after
if (maybeNullObject is not null)
    request.AddJsonBody(maybeNullObject);
Defensive patterns

Strategy: validation

Validate before calling

if (body is null) throw new ArgumentNullException(nameof(body));
// For custom serializers, guarantee non-null output:
// public string? Serialize(BodyParameter b) => JsonSerializer.Serialize(b.Value) ?? string.Empty;
request.AddJsonBody(body);

Try / catch

try {
    await client.ExecuteAsync(request);
}
catch (System.Runtime.Serialization.SerializationException ex) when (ex.Message.Contains("serialized to null")) {
    // body serializer returned null; fix serializer/body before retry
    logger.LogError(ex, "Body serialized to null");
}

Prevention

When it happens

Trigger: Adding a body with AddJsonBody/AddXmlBody (or AddBody with a Json/Xml DataFormat) where the serializer's Serialize method returns null. This happens when the body object is null, or a custom IRestSerializer implementation returns null from Serialize, or the value type cannot be serialized by the configured serializer.

Common situations: Passing a null object to AddJsonBody/AddXmlBody; registering a custom serializer that returns null for certain types; using a DataFormat whose serializer produces null for an empty/default object. Also seen after upgrading RestSharp where serializer registration changed.

Related errors


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