{"record":{"id":"d263c887e75b933b","repo":"restsharp/RestSharp","slug":"failed-to-put-file-as-content-because-flag-alwaysm","errorCode":null,"errorMessage":"Failed to put file as content because flag AlwaysMultipartFormData is enabled","messagePattern":"Failed to put file as content because flag AlwaysMultipartFormData is enabled","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Request/RestRequestExtensions.File.cs","lineNumber":79,"sourceCode":"        /// <param name=\"options\">File parameter header options</param>\n        /// <returns></returns>\n        public RestRequest AddFile(\n            string                name,\n            Func<Stream>          getFile,\n            string                fileName,\n            ContentType?          contentType = null,\n            FileParameterOptions? options     = null\n        )\n            => request.AddFile(FileParameter.Create(name, getFile, fileName, contentType, options));\n\n        internal void ValidateParameters() {\n            if (!request.AlwaysSingleFileAsContent) return;\n\n            var postParametersExists = request.Parameters.GetContentParameters(request.Method).Any();\n            var bodyParametersExists = request.Parameters.Any(p => p.Type == ParameterType.RequestBody);\n\n            if (request.AlwaysMultipartFormData)\n                throw new ArgumentException(\"Failed to put file as content because flag AlwaysMultipartFormData is enabled\");\n\n            if (postParametersExists)\n                throw new ArgumentException(\"Failed to put file as content because POST parameters were added\");\n\n            if (bodyParametersExists)\n                throw new ArgumentException(\"Failed to put file as content because body parameters were added\");\n        }\n    }\n}","sourceCodeStart":61,"sourceCodeEnd":88,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Request/RestRequestExtensions.File.cs#L61-L88","documentation":"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.","triggerScenarios":"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.","commonSituations":"Copy-pasting options from a multipart upload example into a single-file-content flow; toggling flags during experimentation without resetting the conflicting one.","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."],"exampleFix":"// before\nrequest.AlwaysSingleFileAsContent = true;\nrequest.AlwaysMultipartFormData   = true; // throws on AddFile/Execute\n\n// after\nrequest.AlwaysSingleFileAsContent = true;\nrequest.AlwaysMultipartFormData   = false;","handlingStrategy":"validation","validationCode":"if (request.AlwaysSingleFileAsContent && request.AlwaysMultipartFormData)\n    throw new InvalidOperationException(\"Cannot enable both AlwaysSingleFileAsContent and AlwaysMultipartFormData\");\nrequest.AddFile(name, bytes, fileName);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["file-upload","request","configuration","multipart","mutual-exclusion","argument"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}