6

I am trying to request a GET using SSC ODataController with Sitecore 8.2 and it keeps throwing a 406 Not Acceptable error. URL - http://sc82/sitecore/ssc/api/example-controller/products

This is the sample ODataController code:

 public class ProductsController : ServiceBaseODataController<Product>
 {
    protected IReadOnlyEntityRepository<Product> ProductRepository { get; private set; }

    public ProductsController(IReadOnlyEntityRepository<Product> repository) : base(repository)
    {
    }

    public ProductsController() : this(new ProductRepository())
    {   
    }

    public override async Task<IHttpActionResult> Get()
    {  
        IQueryable<Product> result = await ProductRepository.GetData();           
        return Ok(result);    
    }

}

Please let me know if anyone has tried using ODataController with Sitecore 8.2 and faced similar issue.

  • 1
    Can you check your request to the controller and especially the accept-header? A "406 Not Acceptable" normally means that the client (your browser probably) indicated through the accept-header what the characteristics of the data it will accept back should be. If the webserver detects that it will send something that is not acceptable, it will return a 406. – Gatogordo Oct 22 '16 at 23:05
  • Thanks @Gatogordo. The accept header goes as "text/html" but that is default by Sitecore and I am not sure if that has to be explicitly changed somehow. – Mohit Oct 23 '16 at 1:13
  • 1
    Can you give more details about the request? Where and how are you using the controller? – Gatogordo Oct 23 '16 at 8:56
  • The above source code is what I have for my Controller and deployed to my /Website/bin and accessing it through REST Client with URL: sc82/sitecore/api/ssc/Api-Example-Controllers/Products. The break point never hits the "Get" Action Method – Mohit Oct 24 '16 at 2:24
4

The pure HTTP GET request to your ProductController, assuming it was registered by the aggregate descriptor as being part of the custom service, should look something like:

GET /sitecore/api/ssc/aggregate/custom/Products HTTP/1.1 Host: test.site Accept: application/json;

Via cUrl you would be able to request your Products:

curl -X GET -H "Accept: application/json;" "http://test.site/sitecore/api/ssc/aggregate/custom/Products"

If in doubt SSC provides a discovery service where you can identiy the aggregate services registered within a website.

curl -X GET -H "Accept: application/json;" "http://test.site/sitecore/api/ssc/aggregate/"

To retrieve the EDM metadata about your services fire a GET request at the sitecore/api/ssc/aggregate/custom/$metadata route.

Hope that helps :)

  • Thanks @Kevin. Though before your input, I tried the WebApiConfig.cs to tie up through Sitecore initialize pipeline and added this – Mohit Oct 25 '16 at 2:14
  • config.MapODataServiceRoute("ODataRoute", "sitecore/api/ssc/SCS-Api-Example-Controllers", builder.GetEdmModel()); This returned the JSON response. Quick Question - How do we register to aggregate descriptor? – Mohit Oct 25 '16 at 2:16
  • @Mohit derive a class from AggregateDescriptor and the SSC initialisation will do the registering for you using your class. There's an example in my SSC.AggregateService GitHub repos here – Kevin Obee Mar 31 '17 at 14:22
2

A client (e.g. your browser) can indicate to the webserver the characteristics of the data it will accept back from the webserver. This is done using 'accept headers' of the following types:

  • Accept: The MIME types accepted by the client. For example, a browser may only accept back types of data (HTML files, GIF files etc.) it knows how to process.
  • Accept-Charset, Accept-Encoding, Accept-Language, Accept-Ranges

If the Web server detects that the data it wants to return is not acceptable to the client, it returns a header containing the 406 error code

If you test with the browser, the accept header has nothing to do with Sitecore. If your accept header only states text/html, it is normal that the data from your controller is giving a 406 because that is not html. The default accept header in my Chrome for example is "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8". With these values I can retrieve data from controllers without problems.

More information on 406 can be found here - a complete reference to the accept headers here.

Some possible things to do:

  • test with another browser
  • check why your browser is sending just "text/html" as accept header and try to change that

An overview of what the default accept headers should be can be found here.

  • Thank you but i tried modifying the Accept header as "application/json" through Fiddler and it still returns 406. There were other reasons for 406 I found - not to use System.Web.Http.OData and instead use System.Web.OData references. I have tried most of the possible options but no luck... – Mohit Oct 24 '16 at 15:01
  • Did you try with an accept header / to accept everything (just to test) ? – Gatogordo Oct 24 '16 at 15:35
  • yes I did, and I also tried adding JSON to the response as mandatory through WebApiConfig.cs. I've a feeling this has something to do with how Sitecore is treating the oData routes. I'll try to test a plain-simple OData WebAPI without Sitecore, – Mohit Oct 24 '16 at 15:41
  • yeah, the plain simple ODataController works when prefixed with "odata" on the URL, like, localhost:2222/odata/Products. But with Sitecore, the default routeprefix is "/sitecore/api/ssc/{namespace}" and this is causing some route issues.. – Mohit Oct 24 '16 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.