Search code examples
c#.netodataodatalib

OData metadata url request fails if I set the version


The following code gets a 500:

WebRequest request =
  WebRequest.Create("https://services.odata.org/TripPinRESTierService/$metadata");
HttpWebRequest webRequest = request as HttpWebRequest;
webRequest.Accept = "application/atom+xml,application/xml";
request.Headers.Add("DataServiceVersion", "4.0");
request.Headers.Add("OData-MaxVersion", "4.0");
webRequest.AutomaticDecompression =
  DecompressionMethods.Deflate | DecompressionMethods.GZip;

// this throws the 500
var response = request.GetResponse();

But this succeeds:

WebRequest request =
   WebRequest.Create("https://services.odata.org/TripPinRESTierService/$metadata");
HttpWebRequest webRequest = request as HttpWebRequest;
webRequest.Accept = "application/atom+xml,application/xml";
request.Headers.Add("OData-MaxVersion", "4.0");
webRequest.AutomaticDecompression =
  DecompressionMethods.Deflate | DecompressionMethods.GZip;
var response = request.GetResponse();

This throws error:

WebRequest request = 
  WebRequest.Create("https://services.odata.org/TripPinRESTierService/Airlines");
HttpWebRequest webRequest = request as HttpWebRequest;
webRequest.Accept = "application/atom+xml,application/xml";
request.Headers.Add("DataServiceVersion", "4.0");
request.Headers.Add("OData-MaxVersion", "4.0");
webRequest.AutomaticDecompression =
  DecompressionMethods.Deflate | DecompressionMethods.GZip;

// this throws the 500
var response = request.GetResponse();

Is there something special where I should not specify the DataServiceVersion when requesting the metadata?


Solution

  • According to the docs, the header for the OData-version, is OData-Version, not DataServiceVersion.

    Based on my test using Postman, this is what was throwing the 500 error.

    And BTW, consider using HttpClient instead of WebClient, works asynchronously, and is lighter than WebClient. See this and this.