Skip to content

Commit e71d9cc

Browse files
committed
Add binary format
1 parent c9b8cfa commit e71d9cc

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/RestSharp/Enum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public enum ParameterType {
2727
/// <summary>
2828
/// Data formats
2929
/// </summary>
30-
public enum DataFormat { Json, Xml, None }
30+
public enum DataFormat { Json, Xml, Binary, None }
3131

3232
/// <summary>
3333
/// HTTP method to use when making requests

src/RestSharp/Parameters/BodyParameter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ namespace RestSharp;
1818
public record BodyParameter : Parameter {
1919
public BodyParameter(string? name, object value, string contentType, DataFormat dataFormat = DataFormat.None)
2020
: base(name, Ensure.NotNull(value, nameof(value)), ParameterType.RequestBody, false) {
21+
if (dataFormat == DataFormat.Binary && value is not byte[]) {
22+
throw new ArgumentException("Binary data format needs a byte array as value");
23+
}
2124
ContentType = contentType;
2225
DataFormat = dataFormat;
2326
}
@@ -26,6 +29,11 @@ public BodyParameter(string? name, object value, string contentType, DataFormat
2629
/// Body parameter data type
2730
/// </summary>
2831
public DataFormat DataFormat { get; init; } = DataFormat.None;
32+
33+
/// <summary>
34+
/// Custom content encoding
35+
/// </summary>
36+
public string? ContentEncoding { get; init; }
2937
}
3038

3139
public record XmlParameter : BodyParameter {

src/RestSharp/Request/RequestContent.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,23 @@ void AddFiles() {
6868

6969
HttpContent Serialize(BodyParameter body) {
7070
return body.DataFormat switch {
71-
DataFormat.None => new StringContent(body.Value!.ToString()!, _client.Options.Encoding, body.ContentType),
72-
_ => GetSerialized()
71+
DataFormat.None => new StringContent(body.Value!.ToString()!, _client.Options.Encoding, body.ContentType),
72+
DataFormat.Binary => GetBinary(),
73+
_ => GetSerialized()
7374
};
7475

76+
HttpContent GetBinary() {
77+
var byteContent = new ByteArrayContent((body.Value as byte[])!);
78+
byteContent.Headers.ContentType = MediaTypeHeaderValue.Parse(body.ContentType);
79+
80+
if (body.ContentEncoding != null) {
81+
byteContent.Headers.ContentEncoding.Clear();
82+
byteContent.Headers.ContentEncoding.Add(body.ContentEncoding);
83+
}
84+
85+
return byteContent;
86+
}
87+
7588
HttpContent GetSerialized() {
7689
if (!_client.Serializers.TryGetValue(body.DataFormat, out var serializerRecord))
7790
throw new InvalidDataContractException(
@@ -120,7 +133,7 @@ void AddBody(bool hasPostParameters) {
120133
}
121134

122135
if (_client.Options.DisableCharset) {
123-
Content.Headers.ContentType.CharSet = "";
136+
Content.Headers.ContentType!.CharSet = "";
124137
}
125138
}
126139

0 commit comments

Comments
 (0)