Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Work around bugs in HttpClient and long form POST data. See: #1814
  • Loading branch information
kendallb committed Apr 4, 2022
commit b5feec805a1631602aa887d4b84f98e56579bf93
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