Skip to content

Commit 887c566

Browse files
committed
Revert the behaviour of using content type as body parameter name in the content disposition header.
1 parent d6baf06 commit 887c566

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/RestSharp/Request/RequestContent.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,16 @@ void AddBody(bool hasPostParameters) {
128128
var bodyContent = Serialize(bodyParameter!);
129129

130130
// we need to send the body
131-
if (hasPostParameters || _request.HasFiles() || BodyShouldBeMultipartForm(bodyParameter!)) {
131+
if (hasPostParameters || _request.HasFiles() || BodyShouldBeMultipartForm(bodyParameter!) || _request.AlwaysMultipartFormData) {
132132
// here we must use multipart form data
133133
var mpContent = Content as MultipartFormDataContent ?? new MultipartFormDataContent();
134+
var ct = bodyContent.Headers.ContentType?.MediaType;
135+
var name = bodyParameter!.Name.IsEmpty() ? ct : bodyParameter!.Name;
134136

135-
if (bodyParameter!.Name.IsEmpty())
137+
if (name.IsEmpty())
136138
mpContent.Add(bodyContent);
137139
else
138-
mpContent.Add(bodyContent, bodyParameter.Name!);
140+
mpContent.Add(bodyContent, name!);
139141
Content = mpContent;
140142
}
141143
else {

test/RestSharp.Tests.Integrated/MultipartFormDataTests.cs

+24-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public async Task MultipartFormData() {
8585
AddParameters(request);
8686

8787
string boundary = null;
88+
8889
request.OnBeforeRequest = http => {
8990
boundary = ((MultipartFormDataContent)http.Content)!.GetFormBoundary();
9091
return default;
@@ -110,6 +111,7 @@ public async Task MultipartFormData_HasDefaultContentType() {
110111
request.AddParameter(new BodyParameter("controlName", "test", "application/json"));
111112

112113
string boundary = null;
114+
113115
request.OnBeforeRequest = http => {
114116
boundary = ((MultipartFormDataContent)http.Content)!.GetFormBoundary();
115117
return default;
@@ -139,6 +141,7 @@ public async Task MultipartFormData_WithCustomContentType() {
139141
request.AddParameter(new BodyParameter("controlName", "test", "application/json"));
140142

141143
string boundary = null;
144+
142145
request.OnBeforeRequest = http => {
143146
boundary = ((MultipartFormDataContent)http.Content)!.GetFormBoundary();
144147
return default;
@@ -165,6 +168,7 @@ public async Task MultipartFormData_WithParameterAndFile_Async() {
165168
request.AddParameter(new BodyParameter("controlName", "test", "application/json"));
166169

167170
string boundary = null;
171+
168172
request.OnBeforeRequest = http => {
169173
boundary = ((MultipartFormDataContent)http.Content)!.GetFormBoundary();
170174
return default;
@@ -180,21 +184,21 @@ public async Task MultipartFormData_WithParameterAndFile_Async() {
180184
[Fact]
181185
public async Task MultipartFormDataWithBoundaryOverride() {
182186
var request = new RestRequest("/", Method.Post) {
183-
AlwaysMultipartFormData = true,
187+
AlwaysMultipartFormData = true,
184188
FormatMultipartContentType = (ct, b) => $"{ct}; boundary=--------{b}"
185189
};
186190

187191
AddParameters(request);
188192

189193
HttpContent content = null;
190-
var boundary = "";
194+
var boundary = "";
191195

192196
request.OnBeforeRequest = http => {
193197
content = http.Content;
194198
boundary = ((MultipartFormDataContent)http.Content)!.GetFormBoundary();
195199
return default;
196200
};
197-
201+
198202
await _client.ExecuteAsync(request);
199203

200204
var contentType = content.Headers.ContentType!.ToString();
@@ -219,4 +223,21 @@ public async Task MultipartFormDataAsync() {
219223

220224
response.Content.Should().Be(expected);
221225
}
226+
227+
[Fact]
228+
public async Task ShouldHaveJsonContentType() {
229+
var jsonData = new {
230+
Company = "Microsoft",
231+
ZipCode = "LS339",
232+
Country = "USA"
233+
};
234+
235+
var request = new RestRequest {
236+
Method = Method.Post,
237+
AlwaysMultipartFormData = true
238+
};
239+
request.AddJsonBody(jsonData);
240+
241+
var response = await _client.ExecuteAsync(request);
242+
}
222243
}

0 commit comments

Comments
 (0)