Skip to content

Commit 221c467

Browse files
committed
Merge branch 'feature/http-client-issue-1814' of github.com:kendallb/RestSharp into kendallb-feature/http-client-issue-1814
2 parents f3af0ce + 96de5c9 commit 221c467

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/RestSharp/Request/RequestContent.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System.Net;
1516
using System.Net.Http.Headers;
1617
using System.Runtime.Serialization;
1718
using RestSharp.Extensions;
@@ -157,13 +158,16 @@ void AddPostParameters(ParametersCollection? postParameters) {
157158
}
158159
}
159160
else {
160-
// we should not have anything else except the parameters, so we send them as form URL encoded
161-
var formContent = new FormUrlEncodedContent(
162-
_request.Parameters
163-
.Where(x => x.Type == ParameterType.GetOrPost)
164-
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))!
165-
);
166-
Content = formContent;
161+
// we should not have anything else except the parameters, so we send them as form URL encoded. However due
162+
// to bugs in HttpClient FormUrlEncodedContent (see https://github.com/restsharp/RestSharp/issues/1814) we
163+
// do the encoding ourselves using WebUtility.UrlEncode instead.
164+
var formData = _request.Parameters
165+
.Where(x => x.Type == ParameterType.GetOrPost)
166+
.Select(x => new KeyValuePair<string, string>(x.Name!, x.Value!.ToString()!))!;
167+
var encodedItems = formData.Select(i => $"{WebUtility.UrlEncode(i.Key)}={WebUtility.UrlEncode(i.Value)}"/*.Replace("%20", "+")*/);
168+
var encodedContent = new StringContent(string.Join("&", encodedItems), null, "application/x-www-form-urlencoded");
169+
170+
Content = encodedContent;
167171
}
168172
}
169173

test/RestSharp.Tests.Integrated/PostTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Net;
12
using RestSharp.Tests.Integrated.Server;
23

34
namespace RestSharp.Tests.Integrated;
@@ -33,4 +34,19 @@ public async Task Should_post_json_with_PostJsonAsync() {
3334

3435
response.Message.Should().Be(body.Data);
3536
}
37+
38+
class Response {
39+
public string Message { get; set; }
40+
}
41+
42+
[Fact]
43+
public async Task Should_post_large_form_data() {
44+
const int length = 1024 * 1024;
45+
var superLongString = new string('?', length);
46+
var request = new RestRequest("post/form", Method.Post).AddParameter("big_string", superLongString);
47+
var response = await _client.ExecuteAsync<Response>(request);
48+
49+
response.StatusCode.Should().Be(HttpStatusCode.OK);
50+
response.Data!.Message.Should().Be($"Works! Length: {length}");
51+
}
3652
}

test/RestSharp.Tests.Integrated/Server/TestServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public HttpServer(ITestOutputHelper output = null) {
5353

5454
// POST
5555
_app.MapPost("/post/json", (TestRequest request) => new TestResponse { Message = request.Data });
56+
_app.MapPost("/post/form", (HttpContext context) => new TestResponse { Message = $"Works! Length: {context.Request.Form["big_string"].ToString().Length}" });
5657

5758
IResult HandleHeaders(HttpContext ctx) {
5859
var response = ctx.Request.Headers.Select(x => new TestServerResponse(x.Key, x.Value));

0 commit comments

Comments
 (0)