restsharp/RestSharp · error · ArgumentException

Failed to put file as content because body parameters were a

Error message

Failed to put file as content because body parameters were added

What it means

ValidateParameters throws when AlwaysSingleFileAsContent is enabled and the request already has a ParameterType.RequestBody parameter (a body added via AddJsonBody/AddXmlBody/AddStringBody/AddBody). A raw single-file body and a serialized request body cannot both occupy the HTTP body.

Source

Thrown at src/RestSharp/Request/RestRequestExtensions.File.cs:85

            ContentType?          contentType = null,
            FileParameterOptions? options     = null
        )
            => request.AddFile(FileParameter.Create(name, getFile, fileName, contentType, options));

        internal void ValidateParameters() {
            if (!request.AlwaysSingleFileAsContent) return;

            var postParametersExists = request.Parameters.GetContentParameters(request.Method).Any();
            var bodyParametersExists = request.Parameters.Any(p => p.Type == ParameterType.RequestBody);

            if (request.AlwaysMultipartFormData)
                throw new ArgumentException("Failed to put file as content because flag AlwaysMultipartFormData is enabled");

            if (postParametersExists)
                throw new ArgumentException("Failed to put file as content because POST parameters were added");

            if (bodyParametersExists)
                throw new ArgumentException("Failed to put file as content because body parameters were added");
        }
    }
}

View on GitHub (pinned to 6a50821692)

Solutions

  1. Do not add a request body when using AlwaysSingleFileAsContent; the file IS the body.
  2. If you need both a structured body and a file, use multipart form data (remove AlwaysSingleFileAsContent).
  3. Check request.TryGetBodyParameter / Parameters for RequestBody entries before enabling single-file mode.

Example fix

// before
request.AddJsonBody(payload);
request.AlwaysSingleFileAsContent = true;
request.AddFile("file", bytes, "f.bin"); // throws

// after
// drop the JSON body; the file is the body:
request.AlwaysSingleFileAsContent = true;
request.AddFile("file", bytes, "f.bin");
Defensive patterns

Strategy: validation

Validate before calling

if (request.AlwaysSingleFileAsContent) {
    var hasBody = request.Parameters.Any(p => p.Type == ParameterType.RequestBody);
    if (hasBody)
        throw new InvalidOperationException("Remove the request body before single-file-as-content upload");
}
request.AddFile(name, bytes, fileName);

Prevention

When it happens

Trigger: Setting AlwaysSingleFileAsContent = true, adding a body (AddBody/AddJsonBody/AddXmlBody/AddStringBody), then adding a file or executing the request.

Common situations: Adding a JSON payload and then switching the request to a file-content upload without removing the body; building a request generically where a body and a file both get attached.

Related errors


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