text/plain Media Type not being accepted for WebApi v2
Asked Answered
B

1

9

This problem started off with IE9, where for POST requests, the contentType has to be text/plain, and application/json will not work.

I've added the moonscript and proceeded to use contentType: text/plain. I've also added the custom media type to the api, as shown on numerous forms below:

And added the insertion of the text/plain media type to the WebApiConfig

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

// allows 'text/plain' as a supported media type
config.Formatters.Add(new TextMediaTypeFormatter());

However, when posting in IE9 (using emulation), I am still receiving a 415 Unsupported Media Type

Key Value Response HTTP/1.1 415 Unsupported Media Type

$.ajax({
    type: "POST",
    url: hope_forms.viivApiUrl + 'newsletter',
    contentType: 'text/plain',
    data: JSON.stringify(model),
    success: function (data) {
           .....
    },
    error: function (responseText) {
           console.log(responseText)
           modal.showModal('Something went wrong, please try again.');
   }                    
});

Addition:

Here's the full blown WebApiConfig in the event that something is out of order:

var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();

config.EnableSystemDiagnosticsTracing();


//config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

// allows 'text/plain' as a supported media type
config.Formatters.Add(new TextMediaTypeFormatter());

I also changed the ajaxTransport xhr wrapper to use this instead: https://github.com/gfdev/javascript-jquery-transport-xdr


Note:

As of today, 09/21, I've switched all my POST requests to GET, but I still would like a work-around to get these types back to POST.

Boethius answered 7/9, 2016 at 14:40 Comment(6)
Try to change config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); to config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));Gillette
The TextMediaTypeFormatter class adds that (which is in the (2) links that I posted from other forms.Boethius
Just a guess - but after reading the both articles I cannot get the reason why to add text/html to the JsonFormatter. I mean the first line, not the adding of the TextMediaTypeFormatter.Gillette
@Rob Scott please check your web api response in advance rest client or postmen with your publish URL because of when web api throw error then it's return html response [AdvanceRest ](chrome.google.com/webstore/detail/advanced-rest-client/…)Epidaurus
add in ajax call dataType: 'json' or contentType:'application/json' and check your response in chrome advance rest tool i hope it help youEpidaurus
@jayeshdhameliya - depending on whether or not XDomainRequest object is present or not (IE9), the contentType is switched from json to text/plain. Please note that in order to POST w/ IE9, the xhr wrapper is needed.Boethius
A
1

I think you've run into the strange XDomainRequest issue that cropped up in 2014 according to this MSDN blog

Note: As of 2014, XDomainRequest doesn’t appear to send any Content-Type header at all. It’s not clear to me when this changed.

Here's a previous SO question on the topic as well which actually references that blog.

This is also backed up by the documentation for the jQuery extension you are using. In the Readme.md

XDomainRequest have some limitations:

  • there is no Content-Type header in request

So if you check HttpContext.Request.ContentType I'm betting it's going to be null / empty in which case you should be able to assign the response type of "text/plain" and pray to the gods it works.

Basically IE < 10 support for XDomainRequest (and even XDomainRequest itself) is garbage. It has been basically ditched to my understanding and IE 10 implemented CORS support for XHR requests

Alvaroalveolar answered 21/9, 2016 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.