OData 5.2 nightly. Data as XML

Topics: ASP.NET Web API
May 12, 2014 at 12:40 PM
Edited May 19, 2014 at 6:40 AM
Hello everyone.

I'm fairly new to Web API, and as my first project using it, I need to create some OData endpoints.

I've created a new Web API project and followed this guide to create an OData service. However I'm unable to get it to respond with XML. It acknowledges the Accept header/$format parameter, and understands the various levels of metadata, aslong as I'm requesting Json. But as soon as I request xml, it falls back to Json with minimal metadata. Almost as if there's no xml mediaformatter.
Allow me to illustrate.

Request / Reponse:
GET http://localhost:48507/odata/Teams HTTP/1.1
Host: localhost:48507
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal
[additional headers removed for brevity]
Content-Length: 412

{
  "@odata.context":"http://localhost:48507/odata/$metadata#Teams/WebApplication2.Models.Player","value":[
    {
      "@odata.type":"#WebApplication2.Models.Player","Id":1,"TeamId":1,"Name":"Test 1"
    },{
      "@odata.type":"#WebApplication2.Models.Player","Id":2,"TeamId":1,"Name":"Test 2"
    },{
      "@odata.type":"#WebApplication2.Models.Player","Id":3,"TeamId":1,"Name":"Test 3"
    }
  ]
}
Request / Reponse:
GET http://localhost:48507/odata/Teams HTTP/1.1
Host: localhost:48507
Accept: application/json;odata.metadata=none
HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=none
Content-Length: 177

{
  "value":[
    {
      "Id":1,"TeamId":1,"Name":"Test 1"
    },{
      "Id":2,"TeamId":1,"Name":"Test 2"
    },{
      "Id":3,"TeamId":1,"Name":"Test 3"
    }
  ]
}
However if I use "application/atom+xml" or "application/xml" in the Accept header. Data is returned as json with minimal metadata.
Request / Response:
GET http://localhost:48507/odata/Teams HTTP/1.1
Host: localhost:48507
Accept: application/atom+xml
HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=minimal; odata.streaming=true
Content-Length: 412

{
  "@odata.context":"http://localhost:48507/odata/$metadata#Teams/WebApplication2.Models.Player","value":[
    {
      "@odata.type":"#WebApplication2.Models.Player","Id":1,"TeamId":1,"Name":"Test 1"
    },{
      "@odata.type":"#WebApplication2.Models.Player","Id":2,"TeamId":1,"Name":"Test 2"
    },{
      "@odata.type":"#WebApplication2.Models.Player","Id":3,"TeamId":1,"Name":"Test 3"
    }
  ]
}
As I mentioned earlier, it's almost as if the odata service is unaware of xml, and simply falls back to the default when it encounters an "unknown" mediatype. However the regular Web API endpoints (constrollers that derive from ApiController instead of ODataController) have no problem responding with XML.
May 26, 2014 at 8:40 AM
I have finally tracked the reason for this behaviour. It seems that XML content negotiation, for anything but the metadocument, have been purposedly disabled in this build. I'm hoping it will be reenabled before release. But until then I guess I will have to make do with json.

However, it is nice to know that it not because of a bug in my code :)
Coordinator
May 27, 2014 at 4:31 PM
Yup, what you are seeing is expected. OData v4 only has a standardized JSON format at this point (see https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=odata).