{"record":{"id":"bfbe6cef788aa3f0","repo":"restsharp/RestSharp","slug":"failed-to-put-file-as-content-because-post-paramet","errorCode":null,"errorMessage":"Failed to put file as content because POST parameters were added","messagePattern":"Failed to put file as content because POST parameters were added","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Request/RestRequestExtensions.File.cs","lineNumber":82,"sourceCode":"            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":64,"sourceCodeEnd":88,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Request/RestRequestExtensions.File.cs#L64-L88","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the POST/content parameters before using single-file-as-content, or move them to query string / headers / URL segments.","If you need fields plus a file, use multipart (AlwaysMultipartFormData) instead of AlwaysSingleFileAsContent.","Audit request.Parameters for ParameterType.GetOrPost entries before enabling the single-file mode."],"exampleFix":"// before\nrequest.AlwaysSingleFileAsContent = true;\nrequest.AddParameter(\"field\", \"value\"); // post parameter\nrequest.AddFile(\"file\", bytes, \"f.bin\");   // throws\n\n// after\nrequest.AlwaysSingleFileAsContent = true;\n// move fields to query string instead:\nrequest.AddQueryParameter(\"field\", \"value\");\nrequest.AddFile(\"file\", bytes, \"f.bin\");","handlingStrategy":"validation","validationCode":"if (request.AlwaysSingleFileAsContent) {\n    var hasPostParams = request.Parameters.GetContentParameters(request.Method).Any();\n    if (hasPostParams)\n        throw new InvalidOperationException(\"Remove POST parameters before single-file-as-content upload\");\n}\nrequest.AddFile(name, bytes, fileName);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use query parameters or headers for metadata when uploading a file as raw content.","Switch to multipart if you need form fields alongside the file.","Check Parameters for GetOrPost entries before enabling single-file mode."],"tags":["file-upload","request","parameters","configuration","argument"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}