Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the OData catalog for Web API OData v4 in XML

I am trying to get a Web API OData V4 endpoint up and running.

I got it going finally (after removing all the DateTime properties from my endpoint) and now the listing of the entities is in JSON.

I love JSON, but I use LinqPad to test my endpoints. It does not understand the JSON for the listing of the entities that are in my feed.

I have looked and can't seem to find a way to change this back to XML, so I am asking here.

Is there a way to have the listing of entities for a Web API OData v4 feed be in XML rather than JSON?

like image 305
Vaccano Avatar asked Aug 08 '14 19:08

Vaccano


People also ask

Does OData support XML?

OData supports two formats for representing resources, the XML-based Atom format and the JSON format.

How do I get OData service metadata?

Add /$metadata to the URI in the address bar to view the metadata of the OData service. The metadata document displays the relevant information that the OData service provides for an OData client in a CSDL (Common Schema Definition Language).

What is the use of $expand in OData?

OData expand functionality can be used to query related data. For example, to get the Course data for each Enrollment entity, include ?$ expand=course at the end of the request path: This tutorial uses Postman to test the web API.

What is $select in OData?

The $select option specifies a subset of properties to include in the response body. For example, to get only the name and price of each product, use the following query: Console Copy. GET http://localhost/odata/Products?$select=Price,Name.


1 Answers

If you have setup a Web API service using the basic, default configurations that the MS walkthroughs suggest, then the response formatter is already configured and will use json by default, or xml if so prompted.

So the prompt for an xml response usually comes from the client request. At its basic form, the request will contain a Accept: application/atom+xml,application/xml header. If it does not, I believe the Web API response will default to json.

For your particular question, there is an alternative for LinqPad. When you setup your OData connection, the dialog has a Formatter option of Xml or Json, which tells Linqpad to issue the corresponding Accept header in its request.

Using Fiddler to monitor the traffic, if you set the LinqPad OData connection to use the xml formatter, the request contains the headers:

MaxDataServiceVersion: 3.0;NetFx
Accept: application/atom+xml,application/xml

and the response comes back as an atom/xml feed.

If you set the option to use the json formatter, the request contains the headers:

MaxDataServiceVersion: 3.0;NetFx
Accept: application/json;odata=minimalmetadata

and the response comes back as json.

For oData v3, LinqPad handles both just fine.

Edit

This now sounds vaguely familiar with some test I ran a short time ago...I'm not sure if LinqPad supports the v4 protocol yet. It uses WCF Data Services client under the covers, and those libraries stopped at OData v3. And, in fact, as you see from the headers above, the request will only talk to a v3 service.

So your issue is not that the Web Api is not supporting both xml and json. The issue is that LinqPad does not yet connect to a v4 oData service. Using Fiddler or other similar tool, you should be able to request both json and/or xml from the service.

like image 170
mdisibio Avatar answered Oct 22 '22 00:10

mdisibio