Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/RestSharp/Parameters/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static Parameter CreateParameter(string? name, object? value, ParameterTy
ParameterType.GetOrPost => new GetOrPostParameter(name!, value?.ToString(), encode),
ParameterType.UrlSegment => new UrlSegmentParameter(name!, value?.ToString()!, encode),
ParameterType.HttpHeader => new HeaderParameter(name, value?.ToString()),
ParameterType.RequestBody => new BodyParameter(name, value!, Serializers.ContentType.Plain),
ParameterType.RequestBody => new BodyParameter("", value!, string.IsNullOrEmpty(name) ? Serializers.ContentType.Plain : name!),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will break things. Unlike RS 106, the latest version allows you to add multiple body parameters. It works with multipart form data. There, parameters must have names, also body parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure I like the fix either but so much code does it that way :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is: do we want to make it compatible or force people to use the right API. In the latter case, we can throw an exception if the parameter name looks like content type, and the exception would say: "it won't work, use AddStringBody". I can add an overload for AddStringBody that would accept the parameter name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s a good idea. For us it took a long time to figure out what was wrong as I had to inspect the outgoing packets to finally work out it was not sent with the right content type. A logical exception explaining what is wrong would save those folks with the same problem a lot of time.

ParameterType.QueryString => new QueryParameter(name!, value?.ToString(), encode),
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
Expand Down
2 changes: 1 addition & 1 deletion test/RestSharp.Tests.Integrated/MultipartFormDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task AlwaysMultipartFormData_WithParameter_Execute() {
Method = Method.Post
};

request.AddParameter("title", "test", ParameterType.RequestBody);
request.AddParameter("", "test", ParameterType.RequestBody);

var response = await _client.ExecuteAsync(request);

Expand Down
13 changes: 13 additions & 0 deletions test/RestSharp.Tests/RestContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,17 @@ public void RestContent_CaseInsensitiveHeaders() {

httpContent.Headers.ContentType!.MediaType.Should().Be(myContentType);
}

[Fact]
public void RestContent_supports_manual_json_body() {
const string myContentType = "application/json";
const string myJsonString = "[]";

var request = new RestRequest("resource").AddParameter(myContentType, myJsonString, ParameterType.RequestBody);
var content = new RequestContent(new RestClient(), request);

var httpContent = content.BuildContent();

httpContent.Headers.ContentType!.MediaType.Should().Be(myContentType);
}
}