restsharp/RestSharp · error · ArgumentException
Binary data format needs a byte array as value
Error message
Binary data format needs a byte array as value
What it means
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.
Source
Thrown at src/RestSharp/Parameters/BodyParameter.cs:22
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
namespace RestSharp;
public record BodyParameter : Parameter {
public BodyParameter(string? name, object value, ContentType contentType, DataFormat dataFormat = DataFormat.None)
: base(name, Ensure.NotNull(value, nameof(value)), ParameterType.RequestBody, false) {
if (dataFormat == DataFormat.Binary && value is not byte[]) {
throw new ArgumentException("Binary data format needs a byte array as value");
}
ContentType = contentType;
DataFormat = dataFormat;
}
public BodyParameter(object value, ContentType contentType, DataFormat dataFormat = DataFormat.None)
: this("", value, contentType, dataFormat) { }
/// <summary>
/// Body parameter data type
/// </summary>
public DataFormat DataFormat { get; init; } = DataFormat.None;
/// <summary>
/// Custom content encoding
/// </summary>
public string? ContentEncoding { get; init; }View on GitHub (pinned to 6a50821692)
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).
Example fix
// before request.AddBody(fileBase64String, DataFormat.Binary); // after byte[] bytes = Convert.FromBase64String(fileBase64String); request.AddBytesBody(bytes, ContentType.Binary);
Defensive patterns
Strategy: type-guard
Validate before calling
if (dataFormat == DataFormat.Binary && value is not byte[]) throw new ArgumentException("Binary body requires byte[]"); Type guard
static bool IsBinaryCompatible(object? value) => value is byte[];
Try / catch
try { request.AddBody(value, DataFormat.Binary); } catch (ArgumentException ex) when (ex.Message.Contains("byte array")) { /* convert to byte[] first */ } Prevention
- Use request.AddBytesBody(bytes, contentType) for binary uploads.
- Convert strings/streams to byte[] before labeling the body as Binary.
When it happens
Trigger: 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[].
Common situations: 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.
Related errors
- Cannot set request body using default parameters. Use Reques
- Request resource doesn't contain a valid scheme for an empty
- File not found
- Invalid character found in header {type}: {value}
- The specified value is not a valid Host header string.
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/d30b42cb982e2a1a.
Report an issue: GitHub.