{"record":{"id":"07d3a55e9bf3e9d2","repo":"restsharp/RestSharp","slug":"non-string-body-found-with-unsupported-content-typ","errorCode":null,"errorMessage":"Non-string body found with unsupported content type","messagePattern":"Non-string body found with unsupported content type","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Request/RestRequestExtensions.Body.cs","lineNumber":43,"sourceCode":"        /// <returns></returns>\n        /// <exception cref=\"ArgumentException\">Thrown if request body type cannot be resolved</exception>\n        /// <remarks>This method will try to figure out the right content type based on the request data format and the provided content type</remarks>\n        public RestRequest AddBody(object obj, ContentType? contentType = null) {\n            if (contentType == null) {\n                return request.RequestFormat switch {\n                    DataFormat.Json   => request.AddJsonBody(obj, contentType),\n                    DataFormat.Xml    => request.AddXmlBody(obj, contentType),\n                    DataFormat.Binary => request.AddParameter(new BodyParameter(\"\", obj, ContentType.Binary)),\n                    _                 => request.AddParameter(new BodyParameter(\"\", obj.ToString()!, ContentType.Plain))\n                };\n            }\n\n            return\n                obj is string str                  ? request.AddStringBody(str, contentType) :\n                obj is byte[] bytes                ? request.AddParameter(new BodyParameter(\"\", bytes, contentType, DataFormat.Binary)) :\n                contentType.Value.Contains(\"xml\")  ? request.AddXmlBody(obj, contentType) :\n                contentType.Value.Contains(\"json\") ? request.AddJsonBody(obj, contentType) :\n                                                     throw new ArgumentException(\"Non-string body found with unsupported content type\", nameof(obj));\n        }\n\n        /// <summary>\n        /// Adds a string body and figures out the content type from the data format specified. You can, for example, add a JSON string\n        /// using this method as request body, using DataFormat.Json/>\n        /// </summary>\n        /// <param name=\"body\">String body</param>\n        /// <param name=\"dataFormat\"><see cref=\"DataFormat\"/> for the content</param>\n        /// <returns></returns>\n        public RestRequest AddStringBody(string body, DataFormat dataFormat) {\n            var contentType = ContentType.FromDataFormat(dataFormat);\n            request.RequestFormat = dataFormat;\n            return request.AddParameter(new BodyParameter(\"\", body, contentType));\n        }\n\n        /// <summary>\n        /// Adds a string body to the request using the specified content type.\n        /// </summary>","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Request/RestRequestExtensions.Body.cs#L25-L61","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use AddJsonBody or AddXmlBody directly instead of AddBody when you know the format.","If the body is a string, call AddStringBody so it is treated as raw text.","If the body is bytes, pass a byte[] so AddBody routes it as binary.","Set request.RequestFormat first and call AddBody(obj) without a contentType so the DataFormat switch is used."],"exampleFix":"// before\nrequest.AddBody(myObject, ContentType.Plain); // throws\n\n// after\nrequest.AddJsonBody(myObject);","handlingStrategy":"type-guard","validationCode":"switch (obj) {\n    case string s:\n        request.AddStringBody(s, contentType!);\n        break;\n    case byte[] b:\n        request.AddParameter(new BodyParameter(\"\", b, contentType!, DataFormat.Binary));\n        break;\n    default:\n        if (contentType!.Value.Contains(\"json\")) request.AddJsonBody(obj, contentType);\n        else if (contentType.Value.Contains(\"xml\")) request.AddXmlBody(obj, contentType);\n        else throw new ArgumentException($\"Unsupported content type {contentType} for {obj.GetType()}\");\n        break;\n}","typeGuard":"static string ResolveBody(object obj, ContentType contentType) => obj switch {\n    string s  => \"string\",\n    byte[]    => \"binary\",\n    _ when contentType.Value.Contains(\"json\") => \"json\",\n    _ when contentType.Value.Contains(\"xml\")  => \"xml\",\n    _ => throw new ArgumentException(\"Unsupported content type for non-string body\")\n};","tryCatchPattern":null,"preventionTips":["Prefer the explicit AddJsonBody/AddXmlBody/AddStringBody methods over generic AddBody.","Set request.RequestFormat before AddBody so the DataFormat switch is used.","Validate content type contains 'json' or 'xml' for typed object bodies."],"tags":["request-body","content-type","serialization","argument"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}