restsharp/RestSharp · error · ArgumentException

Failed to put file as content because POST parameters were a

Error message

Failed to put file as content because POST parameters were added

What it means

ValidateParameters throws when AlwaysSingleFileAsContent is enabled and the request also has POST/content parameters (parameters that go into the body for POST-like methods, via Parameters.GetContentParameters). A single-file-as-content body cannot coexist with other form/post parameters because there is nowhere to put them.

Source

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

            string                name,
            Func<Stream>          getFile,
            string                fileName,
            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. Remove the POST/content parameters before using single-file-as-content, or move them to query string / headers / URL segments.
  2. If you need fields plus a file, use multipart (AlwaysMultipartFormData) instead of AlwaysSingleFileAsContent.
  3. Audit request.Parameters for ParameterType.GetOrPost entries before enabling the single-file mode.

Example fix

// before
request.AlwaysSingleFileAsContent = true;
request.AddParameter("field", "value"); // post parameter
request.AddFile("file", bytes, "f.bin");   // throws

// after
request.AlwaysSingleFileAsContent = true;
// move fields to query string instead:
request.AddQueryParameter("field", "value");
request.AddFile("file", bytes, "f.bin");
Defensive patterns

Strategy: validation

Validate before calling

if (request.AlwaysSingleFileAsContent) {
    var hasPostParams = request.Parameters.GetContentParameters(request.Method).Any();
    if (hasPostParams)
        throw new InvalidOperationException("Remove POST parameters before single-file-as-content upload");
}
request.AddFile(name, bytes, fileName);

Prevention

When it happens

Trigger: Setting AlwaysSingleFileAsContent = true, adding POST parameters (e.g. AddParameter for body-bound parameters accepted by the HTTP method), then adding a file or executing the request.

Common situations: Converting a form-post request that included field parameters into a raw file upload without removing the fields; adding metadata parameters alongside a single-file content upload.

Related errors


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