ServiceStack / Issues

Issue Tracker for the commercial versions of ServiceStack
11 stars 8 forks source link

Unable to use valid custom IANA MimeTypes in query string format parameter #751

Closed steve-solcast closed 3 years ago

steve-solcast commented 3 years ago

ServiceStack supports the format= query string parameter to set the ResponseContentType.

In creating custom standard types the correct format to use is application/x-somename+format. eg application/x-solcast+json

Unfortunately, HttpRequestExtensions.GetQueryStringContentType overrides any custom format if it contains any of "json", "xml" or "jsv".

This forces you to always use an Accept header to pass any registered custom format.

The offending code is:-

else { format = format.LeftPart('.').ToLowerInvariant(); if (format.Contains("json")) return MimeTypes.Json; if (format.Contains("xml")) return MimeTypes.Xml; if (format.Contains("jsv")) return MimeTypes.Jsv; }

        HostContext.ContentTypes.ContentTypeFormats.TryGetValue(format, out var contentType);

        return contentType;
    }

Perhaps if the HostContext.ContentTypes.ContentTypeFormats.TryGetValue(format, out var contentType);

occured prior to the "else ..." the validly registeredd custom formats would work as expected, but the "attempted fix" code would not be broken.

mythz commented 3 years ago

I've removed the hard coded references to Content Types in this commit so it should be using registered content types by default where you should be able to use ?format=x-somename+format

This change is available from the latest v5.10.5 that's now available on MyGet.