runtime icon indicating copy to clipboard operation
runtime copied to clipboard

CheckMediaTypeFormat throws exception for valid Content Types

Open jaredcnance opened this issue 9 years ago • 14 comments

MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName) (https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/Headers/MediaTypeHeaderValue.cs#L254-L266) throws exception for value "text/html; charset=ISO-8859-4" which is used as an example in the w3 RFC 2616.

But, moreover why is this checked anyways? For example, what if I'd like to include invalid header values in my acceptance tests to ensure the server is responding appropriately? How should I set this up in my tests if I can't do this? This creates a limitation around which scenarios can be reproduced in code.

request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html; charset=ISO-8859-4");

From what I can tell this method disallows ANY media type parameters which doesn't make much sense to me.

jaredcnance avatar Feb 18 '17 21:02 jaredcnance

dotnet/corefx#16292 would allow media type parameters, but again I think the solution should be to remove this check or provide an alternative mechanism for creating requests which may be invalid.

jaredcnance avatar Feb 18 '17 21:02 jaredcnance

or provide an alternative mechanism for creating requests which may be invalid.

For example, what if I'd like to include invalid header values in my acceptance tests to ensure the server is responding appropriately? How should I set this up in my tests if I can't do this? This creates a limitation around which scenarios can be reproduced in code. In terms of the strongly typed properties such as ContentType, we don't want to allow invalid formats. The whole point of the strongly typed properties is to produce only valid formats.

You can add headers that have invalid formats to them by using other APIs to add to the header collections for either HttpRequestMessage.Headers or HttpRequestMessage.Content.Headers.

Try using the TryAddWithoutValidation method: https://msdn.microsoft.com/en-us/library/hh875107(v=vs.118).aspx

That should allow you to add the header with the "invalid" format. It will then get sent on the wire as-is.

davidsh avatar Mar 04 '17 03:03 davidsh

@davidsh given that there is a workaround, do we still want to keep it as enhancement for future? What exactly should we enhance?

karelz avatar Apr 05 '17 03:04 karelz

PowerShell repo we have related Issue and we find ourselves in a quandary because left "as is" is bad, skip the check is bad too and make own "full standard" verification is impossible. If CheckMediaTypeFormat reject a standard valid formats this is very strange. Base methods must conform to the standards and expectations of users. InFrom MSDN docs MediaTypeHeaderValue Class:

Represents a media-type as defined in the RFC 2616.

iSazonov avatar Apr 10 '17 04:04 iSazonov

It throws if you pass in the entire content type, but you can set the CharSet like so:

request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
request.Content.Headers.ContentType.CharSet = "ISO-8859-4";

But, I'm not sure what the path would be for specifying media type parameters other than charset. Such as (from the w3c example):

Accept: audio/*; q=0.2, audio/basic

jaredcnance avatar Apr 10 '17 13:04 jaredcnance

I do think that more work needs to be done to bring this into compliance with RFC 2616, as it does say in the docs that it is compliant.

Given that the actual example in the docs

An example of a media-type would be "text/plain; charset=iso-8859-5.

introduces this error - i.e., if I do new MediaTypeHeaderValue("text/plain; charset=iso-8859-5"); I get a FormatException, I'd classify this as a bug, not an enhancement.

gregbair avatar Jan 16 '18 16:01 gregbair

I got around this issue by creating an instance of StringContent and adding the Content-Type header like this

var content = new StringContent( ... );

content.Headers.Remove("Content-Type");
content.Headers.TryAddWithoutValidation("Content-Type", "text/html; charset=ISO-8859-4");

var response = await client.PostAsync(uri, content);

mustakimali avatar Aug 07 '18 13:08 mustakimali

Here's a way to do it without calling functions that have WithoutValidation in the name (note I'm adding both charset and type):

var content = StringContent("{ \"Foo\": \"Bar\" }", Encoding.UTF8);
var mediaType = new MediaTypeHeaderValue("application/atom+xml") {CharSet = "utf-8" } ;
mediaType.Parameters.Add(new NameValueHeaderValue("type", "entry"));
content.Headers.ContentType = mediaType;

ohadschn avatar Feb 27 '19 15:02 ohadschn

How is this still not fixed? @mustakimali your workaround was a life saver

metareven avatar May 27 '20 16:05 metareven

FYI for others struggling with this, the Parse() method does support charset:

var mediaType = MediaTypeHeaderValue.Parse("text/plain; charset=iso-8859-5");

wjrogers avatar Jun 17 '21 15:06 wjrogers

Imo this is clearly a bug and not an enhancement, workarounds are nice if you area dealing with this in your own code, but when it's in a library you don't control its quite annoying. As we can see with the linked RestSharp issue (and the same thing happens with nswag btw). Is there any reason the constructor for MediaTypeHeaderValue should not use the same logic as the static Parse() method? Is the team open to accepting PRs with changes in this direction?

EDIT: took a look at the source code and realized that in the context of MediaTypeHeaderValue the term mediaType refers to just type+subtype ie without parameters and so i guess its not really a "bug" and more of an unforunately misleading design decision which we now have to live with.

alennartz avatar Jun 08 '22 21:06 alennartz

if anyone is curious this is what I had in mind as changes to make the ctor inline with parse. That being said I suspect it might change the semantics of the constructors too much to be the correct fix for this issue.

alennartz avatar Jun 09 '22 21:06 alennartz

Would it make sense for StringContent(string content, Encoding? encoding, string mediaType) to call MediaTypeHeaderValue.Parse rather than new MediaTypeHeaderValue at least?

I can't think of any reason why we would want to forbid someone from passing a content-type which has parameters into StringContent.

This is confusing people in real life, see e.g. this SO issue.

canton7 avatar Dec 01 '22 09:12 canton7

I have a similar issue, we are calling a versioned API which takes the version from the Accept header. Setting this throws an error because application/json; version=1.0 is invalid. it is clear that System.Net.Http.Headers.MediaTypeHeaderValue.CheckMediaTypeFormat is not working as expected when the .Parse is not used.

victor-priceputu-tb avatar Feb 20 '24 13:02 victor-priceputu-tb