restsharp/RestSharp · error · ArgumentException
Failed to put file as content because flag AlwaysMultipartFo
Error message
Failed to put file as content because flag AlwaysMultipartFormData is enabled
What it means
RestRequest.ValidateParameters throws when AlwaysSingleFileAsContent is enabled (sending one file as the raw request body) but AlwaysMultipartFormData is also enabled. The two modes are incompatible: single-file-as-content puts the file bytes directly in the body, while multipart wraps everything in a multipart/form-data envelope.
Source
Thrown at src/RestSharp/Request/RestRequestExtensions.File.cs:79
/// <param name="options">File parameter header options</param>
/// <returns></returns>
public RestRequest AddFile(
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
- Decide on one mode: for a single file as raw body keep AlwaysSingleFileAsContent = true and set AlwaysMultipartFormData = false.
- For multipart uploads, leave AlwaysMultipartFormData = true and do not set AlwaysSingleFileAsContent.
- Review all request flags in one place before adding files.
Example fix
// before request.AlwaysSingleFileAsContent = true; request.AlwaysMultipartFormData = true; // throws on AddFile/Execute // after request.AlwaysSingleFileAsContent = true; request.AlwaysMultipartFormData = false;
Defensive patterns
Strategy: validation
Validate before calling
if (request.AlwaysSingleFileAsContent && request.AlwaysMultipartFormData)
throw new InvalidOperationException("Cannot enable both AlwaysSingleFileAsContent and AlwaysMultipartFormData");
request.AddFile(name, bytes, fileName); Prevention
- Never set both AlwaysSingleFileAsContent and AlwaysMultipartFormData on the same request.
- Document each request's intended upload mode in its builder.
- Reset conflicting flags when reusing request configurations.
When it happens
Trigger: Setting both request.AlwaysSingleFileAsContent = true and request.AlwaysMultipartFormData = true, then adding a file (AddFile) which calls ValidateParameters. Also triggered at request execution time via request.ValidateParameters() in the pipeline.
Common situations: Copy-pasting options from a multipart upload example into a single-file-content flow; toggling flags during experimentation without resetting the conflicting one.
Related errors
- Failed to put file as content because body parameters were a
- AdvancedResponseWriter is not null. Only one response writer
- ResponseWriter is not null. Only one response writer can be
- Failed to put file as content because POST parameters were a
- Both BaseUrl and Resource are empty
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/d263c887e75b933b.
Report an issue: GitHub.