Skip to content
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

Work around bugs in HttpClient and long form POST data. #1815

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ class RequestContent : IDisposable {
}
}
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 class PostTests {

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 sealed class HttpServer {

// 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