Repository navigation
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Net.Httphelp wanted[up-for-grabs] Good issue for external contributors
Milestone
Description
API Approval - iteration 2
This is being submitted again for approval because we decided additional APIs were needed.
Copied from proposal in #17036 (comment)
public class StringContent
{
public StringContent(string content);
+ public StringContent(string content, MediaTypeHeaderValue mediaType); // Already approved in iteration 1
public StringContent(string content, Encoding encoding);
public StringContent(string content, Encoding encoding);
public StringContent(string content, Encoding encoding, string mediaType);
+ public StringContent(string content, Encoding encoding, MediaTypeHeaderValue mediaType); // NEW API proposal
}
public class MediaTypeHeaderValue
{
public MediaTypeHeaderValue(string mediaType)
+ public MediaTypeHeaderValue(string mediaType, string charSet); // NEW API proposal
}Previous API approval is here: #17036 (comment)
Original post
Consider the following code ...
string contentType = "application/atom+xml";
string requestPayload = "some data";
var stringContent = new StringContent(requestPayload, Encoding.UTF8, contentType);According to Wireshark, I'm seeing a POST using HttpClient with this ...
Content-Type: application/atom+xml; charset=utf-8
Shouldn't that Content-Type value just be ...
Content-Type: application/atom+xml
?? Confused, because something is choking my Azure Table Storage requests with ...
string stringToSign =
$"{requestMethod}\n\n{contentType}\n{dateInRfc1123Format}\n{canonicalizedResource}";
... and since SharedKeyLite, which only requires the date and resource, works with my dateInRfc1123Format and canonicalizedResource, I've sort of narrowed it down to the contentType of the request (shown above) not matching what I'm putting into the signature sting, namely just application/atom+xml.
Reacted by Joberto Diniz, Gianni Angelozzi and Dai
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Net.Httphelp wanted[up-for-grabs] Good issue for external contributors
Activity
benaadams commentedon Apr 19, 2016
Looks like it will always put something out https://github.com/dotnet/corefx/blob/d0dc5fc099946adc1035b34a8b1f6042eddb0c75/src/System.Net.Http/src/System/Net/Http/StringContent.cs#L29
davidsh commentedon Apr 19, 2016
The
StringContentconstructor follows the same behavior as .NET Framework (Desktop).You can always replace the
Content-Type(or any header) with something else if the default behavior isn't what you want.JonHanna commentedon Apr 19, 2016
It's certainly invalid. RFC 2046 (RFC 6657 is also relevant) allows for the charset parameter as part of content-types only if it's
text/plainor atext/*type that specifies it too allows.guardrex commentedon Apr 19, 2016
@benaadams @JonHanna Thanks for finding the references to both. Even if it were valid (e.g.,
text/plainortext/*), I think if you supply the value explicitly, the value should be honored when the header is created IMHO.I don't know if this is what is choking my ATS REST service calls, but it's possible, since
SharedKeyLiteworks with my date and resource. This is certainly a suspect for my failingSharedKeyrequest failures.@davidsh How? I get an exception if I try to mod the header by adding ...
... throws ...
Here's the hacked code I'm playing with (i.e., not for production ... I'm just hacking around to get the requests working). How would I set the
Content-Typeexplicitly to override theStringContentone given this code ..davidsh commentedon Apr 19, 2016
When modifying "known" headers, you need to use the properties and not the 'DefaultRequestHeaders' method. For request entity-body headers (content), you need to modify the `HttpRequestMessage.Content.Headers1 collection. You are getting an exception telling you that basically.
So, you need code like this:
You can also use methods like this as well:
guardrex commentedon Apr 19, 2016
@davidsh Ah ... ok ... thx ... that got the header squared away ...
And 🎉 YES! 🎉 ... that cleared up the problem with ATS REST API:
SharedKeyworks now! 😄 👍 🍻My issue is solved, but do you want to leave this open given what @JonHanna reported?
davidsh commentedon Apr 19, 2016
We will research this to determine if there are RFC issues. This code has existed for 5+ years and was previously reviewed by our RFC compliance experts. I'll leave this open for now.
JonHanna commentedon Apr 19, 2016
I was incorrect.
While
charsetwas originally defined only for use withtext/*types, there's nothing to stop some particular type in another group using a copy of it, and RFC 3023 did exactly that with it strongly recommended to be used along withapplication/xmland suggested, and then RFC 4287 references that in turn in allowingcharsetwithapplication/atom+xml. So it's not in the slightest invalid to have that parameter with that content type, sorry for my misleading comment.guardrex commentedon Apr 19, 2016
My concern about it remains: If the dev sets a value explicitly on
new StringContent(), they will not be aware thatcharsetis being added.In any case where the header is part of a service-generated hash that will be checked against a supplied signature (without the
charset, since the dev didn't know it was added to the request header), as is the case with Azure Storage REST Service, ... 💥 ... and lost time having to check a lot of little things partially because the Azure REST docs and exception messages are so poor. All the service comes back with is 'your auth header is hosed! ... check the signature.'If the dev sets an explicit value, seems like it should be honored. At least mod the description to note that
charsetwill be added automatically. It's possible that if I saw that pop-up in VS when I was laying down the code, it might have triggered more immediate interest in tryingMediaTypeHeaderValue().guardrex commentedon Apr 19, 2016
Just for completeness, I checked the Azure Service REST Service using the default ...
... but supplying the content type to the signature string as ...
That works. It's still awfully cryptic tho without a little help.
darrelmiller commentedon May 13, 2016
@JonHanna Usually it is the media type registration document that defines what parameters are allowed. In the application/atom+xml the registration refers explicitly back RFC3023 for the rules, as you stated.
Interestingly, the .net stack loves adding charset onto
application/jsonbut in that case charset is not defined by the media type registration. In fact RFC 7158 goes on to say,It would be awesome if this could be fixed.
davidsh commentedon May 13, 2016
We plan to fix this in CoreFx. We need to study if porting this to .NET Framework (Desktop) will have any app-compat issues.
davidsh commentedon Feb 16, 2017
This will require a new overload to the StringContent constructor so that the charset isn't specified by default on the
Content-Typerequest header. So, we'll need an API proposal for that.40 remaining items
geoffkizer commentedon May 25, 2021
I marked this as 6.0 so it will get reviewed in the near future. That doesn't mean this is committed for 6.0.
bartonjs commentedon May 27, 2021
Video
Looks good as proposed. We even discussed the casing on "charSet", and decided it's correct since it matches the existing property.
namespace System.Net.Http.Headers { public class StringContent { public StringContent(string content); + public StringContent(string content, MediaTypeHeaderValue mediaType); // Already approved in iteration 1 public StringContent(string content, Encoding encoding); public StringContent(string content, Encoding encoding); public StringContent(string content, Encoding encoding, string mediaType); + public StringContent(string content, Encoding encoding, MediaTypeHeaderValue mediaType); // NEW API proposal } public class MediaTypeHeaderValue { public MediaTypeHeaderValue(string mediaType) + public MediaTypeHeaderValue(string mediaType, string charSet); // NEW API proposal } }danmoseley commentedon Dec 30, 2021
Is there anyone in this issue interested in offering a PR?
brianmed commentedon Dec 31, 2021
I am currently coding up an attempt at creating the new APIs.
In addition, I will try a PR after done.
stephentoub commentedon Mar 16, 2022
Implemented by #63231