Skip to content

Commit 7b33052

Browse files
Use request body content type instead of default one (#1659)
* Use request body content type instead of default one * Fix the encoding test
1 parent 710d55a commit 7b33052

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

src/RestSharp/RestRequestExtensions.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,25 @@
1818
using RestSharp.Serialization;
1919
using RestSharp.Serializers;
2020

21-
namespace RestSharp
22-
{
23-
internal static class RestRequestExtensions
24-
{
21+
namespace RestSharp {
22+
internal static class RestRequestExtensions {
2523
internal static void SerializeRequestBody(
26-
this IRestRequest request,
24+
this IRestRequest request,
2725
IDictionary<DataFormat, IRestSerializer> restSerializers,
28-
params ISerializer[] serializers
29-
)
30-
{
26+
params ISerializer[] serializers
27+
) {
3128
var body = request.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody);
3229
if (body == null) return;
3330

34-
if (body.DataFormat == DataFormat.None)
35-
{
31+
if (body.DataFormat == DataFormat.None) {
3632
request.Body = new RequestBody(body.ContentType, body.Name, body.Value);
3733
return;
3834
}
3935

4036
var contentType = body.ContentType ?? ContentType.FromDataFormat[body.DataFormat];
4137
var requestSerializer = serializers.FirstOrDefault(x => x != null && x.ContentType == contentType);
4238

43-
if (requestSerializer != null)
44-
{
39+
if (requestSerializer != null) {
4540
request.Body = new RequestBody(
4641
requestSerializer.ContentType,
4742
requestSerializer.ContentType,
@@ -52,14 +47,13 @@ params ISerializer[] serializers
5247

5348
if (!restSerializers.TryGetValue(body.DataFormat, out var serializer))
5449
throw new InvalidDataContractException(
55-
$"Can't find serializer for content type {body.DataFormat}"
50+
$"Can't find serializer for data type {body.DataFormat}"
5651
);
5752

58-
request.Body = new RequestBody(serializer.ContentType, serializer.ContentType, serializer.Serialize(body));
53+
request.Body = new RequestBody(body.ContentType ?? serializer.ContentType, serializer.ContentType, serializer.Serialize(body)!);
5954
}
6055

61-
internal static void AddBody(this IHttp http, RequestBody requestBody)
62-
{
56+
internal static void AddBody(this IHttp http, RequestBody requestBody) {
6357
// Only add the body if there aren't any files to make it a multipart form request
6458
// If there are files or AlwaysMultipartFormData = true, then add the body to the HTTP Parameters
6559
if (requestBody.Value == null) return;
@@ -68,19 +62,17 @@ internal static void AddBody(this IHttp http, RequestBody requestBody)
6862
? requestBody.ContentType
6963
: requestBody.Name;
7064

71-
if (!http.AlwaysMultipartFormData && !http.Files.Any())
72-
{
65+
if (!http.AlwaysMultipartFormData && !http.Files.Any()) {
7366
var val = requestBody.Value;
7467

7568
if (val is byte[] bytes)
7669
http.RequestBodyBytes = bytes;
7770
else
7871
http.RequestBody = requestBody.Value.ToString();
7972
}
80-
else
81-
{
73+
else {
8274
http.Parameters.Add(new HttpParameter(requestBody.Name, requestBody.Value, requestBody.ContentType));
8375
}
8476
}
8577
}
86-
}
78+
}

test/RestSharp.Tests/UrlBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public void Should_build_uri_using_selected_encoding() {
299299
var client = new RestClient("http://example.com/resource");
300300

301301
var expectedDefaultEncoding = new Uri("http://example.com/resource?town=Hiller%C3%B8d");
302-
var expectedIso89591Encoding = new Uri("http://example.com/resource?town=Hiller%F8d");
302+
var expectedIso89591Encoding = new Uri("http://example.com/resource?town=Hiller%f8d");
303303
Assert.Equal(expectedDefaultEncoding, client.BuildUri(request));
304304
// now changing encoding
305305
client.Encoding = Encoding.GetEncoding("ISO-8859-1");

0 commit comments

Comments
 (0)