Skip to content

Commit 9331b58

Browse files
committed
Added an option to disable the charset
1 parent 6b3a4d0 commit 9331b58

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/RestSharp/Request/RequestContent.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ void AddBody(bool hasPostParameters) {
117117
// we don't have parameters, only the body
118118
Content = bodyContent;
119119
}
120+
121+
if (_client.Options.DisableCharset) {
122+
Content.Headers.ContentType.CharSet = "";
123+
}
120124
}
121125

122126
void AddPostParameters(ParametersCollection? postParameters) {

src/RestSharp/RestClientOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public RestClientOptions(string baseUrl) : this(new Uri(Ensure.NotEmptyString(ba
5151
/// running) will be sent along to the server. The default is false.
5252
/// </summary>
5353
public bool UseDefaultCredentials { get; set; }
54+
55+
/// <summary>
56+
/// Set to true if you need the Content-Type not to have the charset
57+
/// </summary>
58+
public bool DisableCharset { get; set; }
5459

5560
#if NETSTANDARD
5661
public DecompressionMethods AutomaticDecompression { get; set; } = DecompressionMethods.GZip;

test/RestSharp.IntegrationTests/RequestBodyTests.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ public class RequestBodyTests : IClassFixture<RequestBodyFixture> {
99

1010
const string NewLine = "\r\n";
1111

12-
const string TextPlainContentType = "text/plain";
13-
const string ExpectedTextContentType = $"{TextPlainContentType}; charset=utf-8";
12+
const string TextPlainContentType = "text/plain";
13+
const string ExpectedTextContentType = $"{TextPlainContentType}; charset=utf-8";
14+
const string ExpectedTextContentTypeNoCharset = TextPlainContentType;
1415

1516
public RequestBodyTests(RequestBodyFixture fixture, ITestOutputHelper output) {
1617
_output = output;
1718
_server = fixture.Server;
1819
}
1920

20-
async Task AssertBody(Method method) {
21-
var client = new RestClient(_server.Url);
21+
async Task AssertBody(Method method, bool disableCharset = false) {
22+
var options = new RestClientOptions(_server.Url) { DisableCharset = disableCharset };
23+
var client = new RestClient(options);
2224

2325
var request = new RestRequest(RequestBodyCapturer.Resource, method) {
2426
OnBeforeRequest = async m => {
@@ -33,7 +35,8 @@ async Task AssertBody(Method method) {
3335

3436
await client.ExecuteAsync(request);
3537

36-
AssertHasRequestBody(ExpectedTextContentType, bodyData);
38+
var expected = disableCharset ? ExpectedTextContentTypeNoCharset : ExpectedTextContentType;
39+
AssertHasRequestBody(expected, bodyData);
3740
}
3841

3942
[Fact]
@@ -48,9 +51,15 @@ async Task AssertBody(Method method) {
4851
[Fact]
4952
public Task Can_Be_Added_To_PATCH_Request() => AssertBody(Method.Patch);
5053

54+
[Fact]
55+
public Task Can_Be_Added_To_POST_Request_NoCharset() => AssertBody(Method.Post, true);
56+
5157
[Fact]
5258
public Task Can_Be_Added_To_POST_Request() => AssertBody(Method.Post);
5359

60+
[Fact]
61+
public Task Can_Be_Added_To_PUT_Request_NoCharset() => AssertBody(Method.Put, true);
62+
5463
[Fact]
5564
public Task Can_Be_Added_To_PUT_Request() => AssertBody(Method.Put);
5665

@@ -108,7 +117,7 @@ public async Task Query_Parameters_With_Json_Body() {
108117
.AddQueryParameter("key", "value");
109118

110119
await client.ExecuteAsync(request);
111-
120+
112121
RequestBodyCapturer.CapturedUrl.ToString().Should().Be($"{_server.Url}Capture?key=value");
113122
RequestBodyCapturer.CapturedContentType.Should().Be("application/json; charset=utf-8");
114123
RequestBodyCapturer.CapturedEntityBody.Should().Be("{\"displayName\":\"Display Name\"}");

0 commit comments

Comments
 (0)