Reputation: 363
I have a WebAPI deployed as a Azure Web App It is .net Framework 4.6.2. I can call it it once (from a variety of sources, including my JavaScript code and Postman) but the second time it gets called I get the following error:
{ "Message": "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.", "ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'ImportSourceLineActionRequest' from content with media type 'application/octet-stream'.", "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException", "StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)" }
Some things to note:
So ... why does my webservice suddenly think I am sending application/octet-stream?
Further, rebooting the Web App fixes the issue ... for one more time.
Javascript code making the call:
const resp = await fetch(callUrl, {
method: 'POST',
body: bodyOjbectStr,
headers:
{
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
API Method that is getting called
[HttpPost]
public void DoSomeWork(DoSomeWorkRequestRequest request)
{
// Do various things
}
WebApiConfig.cs (snippet of)
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// config.Filters.Add(new RequireHttpsAttribute());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new System.Net.Http.Formatting.RequestHeaderMapping("Accept","text/html",StringComparison.InvariantCultureIgnoreCase,true,"application/json"));
}
}
Any help gratefully accepted.
DS
Upvotes: 1
Views: 204
Reputation: 363
For completeness, this problem got solved. It turned out to be a problem with the 2FA we were using. Not sure why it ever worked once and failed the second time, but when IT made some adjustments to 2FA it all worked.
Upvotes: 0