{"record":{"id":"3b476c583d34cb77","repo":"restsharp/RestSharp","slug":"request-body-serialized-to-null","errorCode":null,"errorMessage":"Request body serialized to null","messagePattern":"Request body serialized to null","errorType":"exception","errorClass":"SerializationException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Request/RequestContent.cs","lineNumber":107,"sourceCode":"        };\n\n        HttpContent GetBinary() {\n            var byteContent = new ByteArrayContent((body.Value as byte[])!);\n            byteContent.Headers.ContentType = body.ContentType.AsMediaTypeHeaderValue;\n\n            if (body.ContentEncoding != null) {\n                byteContent.Headers.ContentEncoding.Clear();\n                byteContent.Headers.ContentEncoding.Add(body.ContentEncoding);\n            }\n\n            return byteContent;\n        }\n\n        HttpContent GetSerialized() {\n            var serializer = client.Serializers.GetSerializer(body.DataFormat);\n            var content    = serializer.Serialize(body);\n\n            if (content == null) throw new SerializationException(\"Request body serialized to null\");\n\n            var contentType = body.ContentType.Or(serializer.Serializer.ContentType);\n\n            return new StringContent(content, client.Options.Encoding, contentType.Value);\n        }\n    }\n\n    static bool BodyShouldBeMultipartForm(BodyParameter? bodyParameter) {\n        if (bodyParameter == null) return false;\n\n        var bodyContentType = bodyParameter.ContentType.OrValue(bodyParameter.Name);\n        return bodyParameter.Name.IsNotEmpty() && bodyParameter.Name != bodyContentType;\n    }\n\n    string GetOrSetFormBoundary() => request.FormBoundary ?? (request.FormBoundary = Guid.NewGuid().ToString());\n\n    MultipartFormDataContent CreateMultipartFormDataContent() {\n        var boundary    = GetOrSetFormBoundary();","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Request/RequestContent.cs#L89-L125","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nrequest.AddJsonBody(maybeNullObject);\n\n// after\nif (maybeNullObject is not null)\n    request.AddJsonBody(maybeNullObject);","handlingStrategy":"validation","validationCode":"if (body is null) throw new ArgumentNullException(nameof(body));\n// For custom serializers, guarantee non-null output:\n// public string? Serialize(BodyParameter b) => JsonSerializer.Serialize(b.Value) ?? string.Empty;\nrequest.AddJsonBody(body);","typeGuard":null,"tryCatchPattern":"try {\n    await client.ExecuteAsync(request);\n}\ncatch (System.Runtime.Serialization.SerializationException ex) when (ex.Message.Contains(\"serialized to null\")) {\n    // body serializer returned null; fix serializer/body before retry\n    logger.LogError(ex, \"Body serialized to null\");\n}","preventionTips":["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."],"tags":["serialization","request-body","null","argument"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}