{"record":{"id":"d30b42cb982e2a1a","repo":"restsharp/RestSharp","slug":"binary-data-format-needs-a-byte-array-as-value","errorCode":null,"errorMessage":"Binary data format needs a byte array as value","messagePattern":"Binary data format needs a byte array as value","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/RestSharp/Parameters/BodyParameter.cs","lineNumber":22,"sourceCode":"// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n// \n// http://www.apache.org/licenses/LICENSE-2.0\n// \n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n// \n\nnamespace RestSharp;\n\npublic record BodyParameter : Parameter {\n    public BodyParameter(string? name, object value, ContentType contentType, DataFormat dataFormat = DataFormat.None)\n        : base(name, Ensure.NotNull(value, nameof(value)), ParameterType.RequestBody, false) {\n        if (dataFormat == DataFormat.Binary && value is not byte[]) {\n            throw new ArgumentException(\"Binary data format needs a byte array as value\");\n        }\n\n        ContentType = contentType;\n        DataFormat  = dataFormat;\n    }\n\n    public BodyParameter(object value, ContentType contentType, DataFormat dataFormat = DataFormat.None)\n        : this(\"\", value, contentType, dataFormat) { }\n\n    /// <summary>\n    /// Body parameter data type\n    /// </summary>\n    public DataFormat DataFormat { get; init; } = DataFormat.None;\n\n    /// <summary>\n    /// Custom content encoding\n    /// </summary>\n    public string? ContentEncoding { get; init; }","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp/Parameters/BodyParameter.cs#L4-L40","documentation":"Thrown by the BodyParameter constructor when DataFormat.Binary is specified but the value is not a byte[]. Binary bodies must be raw bytes; any other type is incompatible with the binary format.","triggerScenarios":"Calling request.AddBody(value, DataFormat.Binary) or constructing new BodyParameter(...) with DataFormat.Binary and a value that is a string, stream, or object instead of byte[].","commonSituations":"Passing a base64 string instead of decoded bytes; passing a Stream instead of its buffered contents; assuming AddStringBody with binary flag works; serializing an object and labeling it Binary.","solutions":["Convert the value to byte[] before adding it as a binary body (e.g. File.ReadAllBytes or stream.ToArray()).","Use request.AddBytesBody(bytes, contentType) which correctly accepts a byte[].","For strings, encode to bytes first: Encoding.UTF8.GetBytes(str)."],"exampleFix":"// before\nrequest.AddBody(fileBase64String, DataFormat.Binary);\n\n// after\nbyte[] bytes = Convert.FromBase64String(fileBase64String);\nrequest.AddBytesBody(bytes, ContentType.Binary);","handlingStrategy":"type-guard","validationCode":"if (dataFormat == DataFormat.Binary && value is not byte[]) throw new ArgumentException(\"Binary body requires byte[]\");","typeGuard":"static bool IsBinaryCompatible(object? value) => value is byte[];","tryCatchPattern":"try { request.AddBody(value, DataFormat.Binary); } catch (ArgumentException ex) when (ex.Message.Contains(\"byte array\")) { /* convert to byte[] first */ }","preventionTips":["Use request.AddBytesBody(bytes, contentType) for binary uploads.","Convert strings/streams to byte[] before labeling the body as Binary."],"tags":["body","binary","request","parameter"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}