cancel
Showing results for 
Search instead for 
Did you mean: 

Setting Accept Request Header on ODataModel

Former Member
0 Kudos

Hi, SCN.

I noticed in the OpenUI5 JsDoc Report that some headers are not modifiable by mHeaders when specifying a new sap.ui.model.odata.ODataModel object. These headers are: accept, accept-language, x-csrf-token, MaxDataServiceVersion, DataServiceVersion. Is it possible to adjust the Accept header when binding to my service URL?

Let's say I need to also interface with an ASP.NET WebAPI OData Service (using v1-3 OData packages). I can set my Accept header to "application/json;odata=verbose" in order to return results in a verbose JSON format that OData v2 supports. By default, my OData v3 service will return a "JSON light" response, which causes an endless loop of GET requests to my service from the UI5 library.

Thanks,

Mitch

As an alternative, I can set my custom service to properly return JSON response based on MaxDataServiceVersion. This should be handled out-of-the-box from a v1-3 OData package, but unfortunately ASP.NET Web API OData does not support OData v2. I can handle this with a custom message handler, but that is out of scope for this request, and I already have a solution there. I would rather find a way to set Accept header from my client for the time being.

Accepted Solutions (0)

Answers (1)

Answers (1)

0 Kudos

Hi, Barry.

I'm facing the same situation where I'm trying to integrate a .Net Odata service and running into endless loop. What was your alternative to overcome this.

Appreciate your help.

Thanks,

Srini.

Former Member
0 Kudos

I am creating my OData model using sap.ui.model.odata.v2.ODataModel


var oAppModel = new ODataModel(

    sServiceUrl,

    {

        json: true,

        defaultCountMode: sap.ui.model.odata.CountMode.None,

        useBatch: false

    }

);

this.setModel(oAppModel);

Then on my ASP.NET OData service, I do have a new Message Handler added in my WebApiConfig.

See this GitHub Gist for the handler I'm using: Verbose JSON for v2 from Web API v1-3 OData · GitHub

You can attach this custom handler in your WebApiConfig like:


public static class WebApiConfig

    {

        public static void Register(HttpConfiguration config)

        {

            // ...

            // Check MaxDataServiceVersion and reply with proper JSON response (also validates $format)

            config.MessageHandlers.Add(new FormatSupportHandler());

            // ...

          }

    }

Good luck,

Mitch

Former Member
0 Kudos

While I was able to solve from server-side, in the scenario where you are unable to alter the API, it would be nice to have the ability to modify accept in the v2.ODataModel.