Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Luc van Soest 6 posts 115 karma points
    Oct 04, 2017 @ 10:02
    Luc van Soest
    0

    Random 406 Http errors on REST API's inherited from UmbracoApiController

    I'm working on big Umbraco project (version 7.6.4) . In this project we got several REST API's which inherit from UmbracoApiController.

    These always have been working with no problems at all but since recently we receive at random moments a "406 Not Acceptable"-response from all our REST API's at the same time, the problem lasts until we restart the web-application but then after a few hours the 406's begin to appear again.

    All these REST API's are being called from jQuery code.

    We also have several custom Backoffice API's which inherit from UmbracoAuthorizedJsonController, these work without any problems.

    Anyone who has experienced this before and may have a solution?

  • Adam Wilks 9 posts 81 karma points
    May 09, 2018 @ 15:45
    Adam Wilks
    1

    Did you ever manage to find the root cause of this issue?

    I am currently investigating a very similar problem with 406 status codes being returned only from Api controllers which inherit from UmbracoApiController which seems to occur every few days but only for a very brief period of less than a minute or so.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    May 09, 2018 @ 15:59
    Dan Diplo
    0

    If you look at the source code for the Umbraco API Controllers it doesn't look to me like they have any influence over what response is returned - they basically extend ApiController with a few extra properties.

    See UmbracoApiController.cs and UmbracoApiControllerBase.cs

    Are you doing anything with OData or similar? That's the only thing that I can recall that can cause 406 responses when it's configured.

  • Adam Wilks 9 posts 81 karma points
    May 09, 2018 @ 16:10
    Adam Wilks
    0

    Thanks Dan, I was just taking a look at the source code now actually.

    The only indication we have that this could be Umbraco related is that so far our failed request logs have only shown this happening with API calls to controllers inheriting from UmbracoApiController, and a quick google led me here.

    I was wondering if this might be a manifestation of a problem happening at a lower level somewhere in the request processing pipeline, possibly leading to Umbraco "warming up" again as it seems to at app startup and during that short interval not being able to handle the incoming api requests.

  • Adam Wilks 9 posts 81 karma points
    May 29, 2018 @ 07:50
    Adam Wilks
    1

    We have been working on this issue some more and have determined that the 406 status codes are being returned from API calls because the AJAX call specifies a MIME type of application/json however the request is being redirected to our static HTML startup page due to the application domain recycling, so the 406 status is a bit misleading.

    Microsoft support have advised that we explore this as a potential issue with Umbraco so we will be exploring our logs more thoroughly looking for errors which could be responsible for this.

    Just thought I'd share the update in case anyone else comes across this in the future. Would love to know if the OP had the same experience.

  • Luc van Soest 6 posts 115 karma points
    May 29, 2018 @ 12:16
    Luc van Soest
    100

    I did actually find the cause for the 406-errors I was experiencing and the cause was a silly mistake in my code.

    I had several REST API's in my project. Some needed to return JSON while others needed to return XML.

    The only way I knew before how to force a REST API to return JSON instead of XML is with the following code in my UmbracoApiController:

        protected override void Initialize(global::System.Web.Http.Controllers.HttpControllerContext controllerContext)
        {
            base.Initialize(controllerContext);
            controllerContext.Configuration.Formatters.Remove(controllerContext.Configuration.Formatters.XmlFormatter);
        }
    

    So I removed to XmlFormatter to force the return of JSON code. But what I didn't realise at the time that I was removing the XmlFormatter, globally, for all REST API's in the project. So whenever this code was hit the other REST API that needed to return XML where returning an 406-error because the XmlFormatter had been removed.

    I solved the issue by removing the previous mentioned code from from all my UmbracoApiControllers and adding a WebApiConfig class in de App_Start

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
            config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
        }
    }
    

    So now when I make a call to one of my REST API's I add a "type"-querystring parameter with either the value "xml" or "json" depending on the format of the return data I want to receive.

Please Sign in or register to post replies

Write your reply to:

Draft