restsharp/RestSharp · error · NotSupportedException

Cannot set request body using default parameters. Use Reques

Error message

Cannot set request body using default parameters. Use Request.AddBody() instead.

What it means

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.

Source

Thrown at src/RestSharp/Parameters/DefaultParameters.cs:31

// limitations under the License.
//

using System.Runtime.CompilerServices;

namespace RestSharp;

public sealed class DefaultParameters(ReadOnlyRestClientOptions options) : ParametersCollection {
    /// <summary>
    /// Safely add a default parameter to the collection.
    /// </summary>
    /// <param name="parameter">Parameter to add</param>
    /// <returns></returns>
    /// <exception cref="NotSupportedException"></exception>
    /// <exception cref="ArgumentException"></exception>
    [MethodImpl(MethodImplOptions.Synchronized)]
    public DefaultParameters AddParameter(Parameter parameter) {
        if (parameter.Type == ParameterType.RequestBody)
            throw new NotSupportedException("Cannot set request body using default parameters. Use Request.AddBody() instead.");

        if (!options.AllowMultipleDefaultParametersWithSameName &&
            parameter.Type != ParameterType.HttpHeader &&
            !MultiParameterTypes.Contains(parameter.Type) &&
            this.Any(x => x.Name == parameter.Name)) {
            throw new ArgumentException("A default parameters with the same name has already been added", nameof(parameter));
        }

        Parameters.Add(parameter);

        return this;
    }

    /// <summary>
    /// Safely removes all the default parameters with the given name and type.
    /// </summary>
    /// <param name="name">Parameter name</param>
    /// <param name="type">Parameter type</param>

View on GitHub (pinned to 6a50821692)

Solutions

  1. Add the body to each individual RestRequest via request.AddBody(...) / request.AddJsonBody(...) instead.
  2. If the body is identical across requests, encapsulate it in a factory method that builds a new RestRequest each time.
  3. Remove any RequestBody parameter from default-parameter registration.

Example fix

// before
client.AddDefaultParameter(new BodyParameter(payload, ContentType.Json));

// after
var request = new RestRequest();
request.AddJsonBody(payload);
Defensive patterns

Strategy: validation

Validate before calling

if (parameter.Type == ParameterType.RequestBody) throw new NotSupportedException("Use request.AddBody() for bodies, not client defaults");

Try / catch

try { client.AddDefaultParameter(parameter); } catch (NotSupportedException ex) when (ex.Message.Contains("Cannot set request body using default parameters")) { /* move to request.AddBody */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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