-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
New ctor for FormUrlEncodedContent
accepting Dictionary
#44458
Comments
Tagging subscribers to this area: @dotnet/ncl Issue meta data
|
Triage: Next step -- research if |
@ManickaP can you please look at it when you get a chance, so that we can make decision? Thanks! |
IMO, I prefer var nullableValueDictionary = new Dictionary<string, string?>
{
["foo"] = default //use easier syntax sugar instead of string.Empty(or "" which is possible to make a typo like " ") and don't worry about exceptions
};
_ = new FormUrlEncodedContent(nullableValueDictionary); @karelz @ManickaP I can make a PR to finish it if you have more imporant things to deal with. BTW, I think Encode here can be optimized. // Escape spaces as '+'.
return Uri.EscapeDataString(data).Replace("%20", "+"); Since this method will be used every time on the keys and values, may String.Replace be removed if we have another method for escaping data string? It's just due to the difference of treating WHITESPACE by W3C and by RFC 2396. Need I to create a stand-alone issue about it? |
Triage: Some more tests are needed to determine if we need new API - @ManickaP is on point. |
Background and Motivation
#38494
Once we annotated
FormUrlEncodedContent
we have prevented people from using the ctor withDictionary
.Dictionary
'sTKey
cannot be null, colliding with key nullability inIEnumerable<KeyValuePair<string?, string?>>
.Changing the existing API might be problematic since the type have always accepted
null
keys and replaces them with an empty string:runtime/src/libraries/System.Net.Http/src/System/Net/Http/FormUrlEncodedContent.cs
Line 37 in 5f96636
Proposal is to add a new constructor accepting
IDictionary<string, string?>
and thus enabling uses like:Proposed API
namespace System.Net.Http { public partial class FormUrlEncodedContent : ByteArrayContent { + public FormUrlEncodedContent(IDictionary<string, string?> dictionary); // Existing ctor public FormUrlEncodedContent(IEnumerable<KeyValuePair<string?, string?>> nameValueCollection); ... } }
Alternative Designs
Making the value non-null as well will ease the usage even more. Enabling usage with
Dictionary<string, string>
:Api proposal:
namespace System.Net.Http { public partial class FormUrlEncodedContent : ByteArrayContent { // non-nullable value as well, we cannot overload on nullability so we cannot add this later, we must chose one + public FormUrlEncodedContent(IDictionary<string, string> dictionary); // Existing ctor public FormUrlEncodedContent(IEnumerable<KeyValuePair<string?, string?>> nameValueCollection) ... } }
Usage Examples
The text was updated successfully, but these errors were encountered: