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
- Do not pass null as the body object; guard with a null check before AddJsonBody/AddXmlBody.
- If using a custom IRestSerializer, ensure Serialize never returns null (return an empty string or throw a meaningful exception instead).
- Verify the correct DataFormat/serializer is registered for the body type via ConfigureSerialization.
- 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
- Never pass null to AddJsonBody/AddXmlBody.
- When registering a custom IRestSerializer, ensure Serialize never returns null.
- Unit-test serializers against all body types you send.
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
- Parameter value is null
- Non-string body found with unsupported content type
- Failed to put file as content because body parameters were a
- Supplied parameter is not an XML parameter
- Class cannot have two properties marked with SerializeAs(Con
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/3b476c583d34cb77.
Report an issue: GitHub.