{"record":{"id":"030d18585d7f8a37","repo":"restsharp/RestSharp","slug":"failed-to-put-file-as-content-because-body-paramet","errorCode":null,"errorMessage":"Failed to put file as content because body parameters were added","messagePattern":"Failed to put file as content because body parameters were added","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Request/RestRequestExtensions.File.cs","lineNumber":85,"sourceCode":"            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":67,"sourceCodeEnd":88,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Request/RestRequestExtensions.File.cs#L67-L88","documentation":"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.","triggerScenarios":"Setting AlwaysSingleFileAsContent = true, adding a body (AddBody/AddJsonBody/AddXmlBody/AddStringBody), then adding a file or executing the request.","commonSituations":"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.","solutions":["Do not add a request body when using AlwaysSingleFileAsContent; the file IS the body.","If you need both a structured body and a file, use multipart form data (remove AlwaysSingleFileAsContent).","Check request.TryGetBodyParameter / Parameters for RequestBody entries before enabling single-file mode."],"exampleFix":"// before\nrequest.AddJsonBody(payload);\nrequest.AlwaysSingleFileAsContent = true;\nrequest.AddFile(\"file\", bytes, \"f.bin\"); // throws\n\n// after\n// drop the JSON body; the file is the body:\nrequest.AlwaysSingleFileAsContent = true;\nrequest.AddFile(\"file\", bytes, \"f.bin\");","handlingStrategy":"validation","validationCode":"if (request.AlwaysSingleFileAsContent) {\n    var hasBody = request.Parameters.Any(p => p.Type == ParameterType.RequestBody);\n    if (hasBody)\n        throw new InvalidOperationException(\"Remove the request body before single-file-as-content upload\");\n}\nrequest.AddFile(name, bytes, fileName);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["The file is the body in single-file-content mode; do not add another body.","Use multipart if a structured body and a file must coexist.","Call request.TryGetBodyParameter to detect an existing body."],"tags":["file-upload","request-body","request","configuration","mutual-exclusion","argument"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}