Skip to content

Use request body content type instead of default one #1659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions src/RestSharp/RestRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,25 @@
using RestSharp.Serialization;
using RestSharp.Serializers;

namespace RestSharp
{
internal static class RestRequestExtensions
{
namespace RestSharp {
internal static class RestRequestExtensions {
internal static void SerializeRequestBody(
this IRestRequest request,
this IRestRequest request,
IDictionary<DataFormat, IRestSerializer> restSerializers,
params ISerializer[] serializers
)
{
params ISerializer[] serializers
) {
var body = request.Parameters.FirstOrDefault(p => p.Type == ParameterType.RequestBody);
if (body == null) return;

if (body.DataFormat == DataFormat.None)
{
if (body.DataFormat == DataFormat.None) {
request.Body = new RequestBody(body.ContentType, body.Name, body.Value);
return;
}

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

if (requestSerializer != null)
{
if (requestSerializer != null) {
request.Body = new RequestBody(
requestSerializer.ContentType,
requestSerializer.ContentType,
Expand All @@ -52,14 +47,13 @@ params ISerializer[] serializers

if (!restSerializers.TryGetValue(body.DataFormat, out var serializer))
throw new InvalidDataContractException(
$"Can't find serializer for content type {body.DataFormat}"
$"Can't find serializer for data type {body.DataFormat}"
);

request.Body = new RequestBody(serializer.ContentType, serializer.ContentType, serializer.Serialize(body));
request.Body = new RequestBody(body.ContentType ?? serializer.ContentType, serializer.ContentType, serializer.Serialize(body)!);
}

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

if (!http.AlwaysMultipartFormData && !http.Files.Any())
{
if (!http.AlwaysMultipartFormData && !http.Files.Any()) {
var val = requestBody.Value;

if (val is byte[] bytes)
http.RequestBodyBytes = bytes;
else
http.RequestBody = requestBody.Value.ToString();
}
else
{
else {
http.Parameters.Add(new HttpParameter(requestBody.Name, requestBody.Value, requestBody.ContentType));
}
}
}
}
}
2 changes: 1 addition & 1 deletion test/RestSharp.Tests/UrlBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void Should_build_uri_using_selected_encoding() {
var client = new RestClient("http://example.com/resource");

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