Skip to content

Commit

Permalink
Merge branch 'feature/http-client-issue-1814' of github.com:kendallb/…
Browse files Browse the repository at this point in the history
…RestSharp into kendallb-feature/http-client-issue-1814
  • Loading branch information
alexeyzimarev committed Jun 7, 2022
2 parents f3af0ce + 96de5c9 commit 221c467
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/RestSharp/Request/RequestContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

Content = encodedContent;
}
}

Expand Down
16 changes: 16 additions & 0 deletions test/RestSharp.Tests.Integrated/PostTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Net;
using RestSharp.Tests.Integrated.Server;

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

response.Message.Should().Be(body.Data);
}

class Response {
public string Message { get; set; }
}

[Fact]
public async Task Should_post_large_form_data() {
const int length = 1024 * 1024;
var superLongString = new string('?', length);
var request = new RestRequest("post/form", Method.Post).AddParameter("big_string", superLongString);
var response = await _client.ExecuteAsync<Response>(request);

response.StatusCode.Should().Be(HttpStatusCode.OK);
response.Data!.Message.Should().Be($"Works! Length: {length}");
}
}
1 change: 1 addition & 0 deletions test/RestSharp.Tests.Integrated/Server/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public HttpServer(ITestOutputHelper output = null) {

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

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

0 comments on commit 221c467

Please sign in to comment.