{"record":{"id":"672c4c32fbde785b","repo":"restsharp/RestSharp","slug":"cannot-set-request-body-using-default-parameters","errorCode":null,"errorMessage":"Cannot set request body using default parameters. Use Request.AddBody() instead.","messagePattern":"Cannot set request body using default parameters\\. Use Request\\.AddBody\\(\\) instead\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Parameters/DefaultParameters.cs","lineNumber":31,"sourceCode":"// limitations under the License.\n//\n\nusing System.Runtime.CompilerServices;\n\nnamespace RestSharp;\n\npublic sealed class DefaultParameters(ReadOnlyRestClientOptions options) : ParametersCollection {\n    /// <summary>\n    /// Safely add a default parameter to the collection.\n    /// </summary>\n    /// <param name=\"parameter\">Parameter to add</param>\n    /// <returns></returns>\n    /// <exception cref=\"NotSupportedException\"></exception>\n    /// <exception cref=\"ArgumentException\"></exception>\n    [MethodImpl(MethodImplOptions.Synchronized)]\n    public DefaultParameters AddParameter(Parameter parameter) {\n        if (parameter.Type == ParameterType.RequestBody)\n            throw new NotSupportedException(\"Cannot set request body using default parameters. Use Request.AddBody() instead.\");\n\n        if (!options.AllowMultipleDefaultParametersWithSameName &&\n            parameter.Type != ParameterType.HttpHeader &&\n            !MultiParameterTypes.Contains(parameter.Type) &&\n            this.Any(x => x.Name == parameter.Name)) {\n            throw new ArgumentException(\"A default parameters with the same name has already been added\", nameof(parameter));\n        }\n\n        Parameters.Add(parameter);\n\n        return this;\n    }\n\n    /// <summary>\n    /// Safely removes all the default parameters with the given name and type.\n    /// </summary>\n    /// <param name=\"name\">Parameter name</param>\n    /// <param name=\"type\">Parameter type</param>","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Parameters/DefaultParameters.cs#L13-L49","documentation":"Thrown by DefaultParameters.AddParameter when the parameter type is ParameterType.RequestBody. Request bodies are per-request constructs and cannot be shared as defaults across all requests, so adding one as a default is explicitly rejected.","triggerScenarios":"Calling client.AddDefaultParameter with a body parameter, or constructing a BodyParameter and adding it via AddDefaultParameter. Any attempt to register a RequestBody-typed parameter on the client-level defaults triggers this.","commonSituations":"Confusing AddDefaultParameter with request.AddBody; trying to set a shared JSON body for all requests through defaults; migrating code that mistakenly used defaults for bodies.","solutions":["Add the body to each individual RestRequest via request.AddBody(...) / request.AddJsonBody(...) instead.","If the body is identical across requests, encapsulate it in a factory method that builds a new RestRequest each time.","Remove any RequestBody parameter from default-parameter registration."],"exampleFix":"// before\nclient.AddDefaultParameter(new BodyParameter(payload, ContentType.Json));\n\n// after\nvar request = new RestRequest();\nrequest.AddJsonBody(payload);","handlingStrategy":"validation","validationCode":"if (parameter.Type == ParameterType.RequestBody) throw new NotSupportedException(\"Use request.AddBody() for bodies, not client defaults\");","typeGuard":null,"tryCatchPattern":"try { client.AddDefaultParameter(parameter); } catch (NotSupportedException ex) when (ex.Message.Contains(\"Cannot set request body using default parameters\")) { /* move to request.AddBody */ }","preventionTips":["Never add RequestBody parameters via client.AddDefaultParameter.","Add bodies per-request with request.AddBody / AddJsonBody."],"tags":["parameters","default-parameters","body","request"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}