Skip to content

Commit b5feec8

Browse files
committed
Work around bugs in HttpClient and long form POST data. See: #1814
1 parent de4a93e commit b5feec8

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-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

0 commit comments

Comments
 (0)