simple-odata-client / Simple.OData.Client

MIT License
331 stars 197 forks source link

Exception: supported MIME type could not be found that matches the content type of the response #591

Open Intagrit-F opened 5 years ago

Intagrit-F commented 5 years ago

Versions used: .net core 2.1 .net 4.7.2 Simple.odata.client 5.6.2

Goal: Execute request to the base uri of a (ODataV2 SAP) service in order to obtain a CSRF (header param) token

Source code: ODataClientSettings settings = new ODataClientSettings(); settings.BaseUri = new Uri("https://host/sap/opu/odata/sap/specificservice/"); String upw = String.Join(":", new String[] { "user", "password" }); var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(upw);

        settings.BeforeRequest = (e) =>
        {
            e.Headers.Add("Accept", "application/xml");
            e.Headers.Add("X-CSRF-Token", "Fetch");
            e.Headers.Add("Authorization", "Basic " + System.Convert.ToBase64String(plainTextBytes));
        };

        idmClient = new ODataClient(settings);

        //request to base service to obtain CSRF (header param) token
        var response = await idmClient.FindEntriesAsync("/");

Fiddler trace: in fact 2 requests are executed

Exception is thrown: Microsoft.AspNetCore.Server.Kestrel|Connection id "0HLL8EM8JRSMH", Request id "0HLL8EM8JRSMH:00000002": An unhandled exception was thrown by the application. Microsoft.Data.OData.ODataContentTypeException: A supported MIME type could not be found that matches the content type of the response. None of the supported type(s) 'application/atom+xml;type=entry, application/atom+xml, application/json;odata=verbose, application/json' matches the content type 'application/xml'.

Issue: The metadata of the service can only be obtained in application/xml but application/xml is not supported by the simple.odata.client?

Does someone have any suggestions how to solve this or is it intended that the library does not support application/xml? A (partial) workaround is to make use of a local (or in application) copy of the metadata: settings.MetadataDocument = System.IO.File.ReadAllText(@"C:\Temp\metadata.xml"); Then the metadata request is not executed. This is a "partial" workaround because a query has to be made on the base uri or the /$metadata in order to obtain a CSRF token.

A workaround for the base uri request in order to obtain a CSRF token: var response = await myClient.GetMediaStreamAsync("");

lucas-garrido commented 3 years ago

This is a "partial" workaround because a query has to be made on the base uri or the /$metadata in order to obtain a CSRF token.

you can request the base url and add /?$format=json to get the csrf token but the problem still the same, the automatic $metadata request result in error